PyObjc: implement NSXPCInterface



我正在写一个扩展和主要应用程序是在PyObjc。我想设置主应用程序和扩展之间的通信。参考链接,我试着写一个协议。

SampleExtensionProtocol = objc.formal_protocol('SampleExtensionProtocol', (), [
objc.selector(None, b"upperCaseString:withReply:", signature=b"v@:@@",isRequired=0),
objc.selector(None, b"setEnableTemperProof:withReply:", signature=b"v@:@@",isRequired=0),
])

连接对象已创建。

connection = NSXPCConnection.alloc().initWithMachServiceName_options_("com.team.extension",NSXPCConnectionPrivileged)

已注册元数据。

objc.registerMetaDataForSelector(b'NSObject', b'upperCaseString:withReply:', {
'arguments': {
3: {
'callable': {
'retval': {'type': b'@'}, 
'arguments': {
0: {'type': b'^v'}, 
1: {'type': b'i'}, 
},
},
}
}
})
objc.registerMetaDataForSelector(b'NSObject', b'setEnableTemperProof:withReply:', {
'arguments': {
3: {
'callable': {
'retval': {'type': b'@'}, 
'arguments': {
0: {'type': b'^v'}, 
1: {'type': b'i'}, 
},
},
}
}
})

但是在创建接口时出现错误。

mySvcIF = Foundation.NSXPCInterface.interfaceWithProtocol_(SampleExtensionProtocol)

ValueError: NSInvalidArgumentException - NSXPCInterface: Unable to get extended method signature from Protocol data (SampleExtensionProtocol / upperCaseString:withReply:). Use of clang is required for NSXPCInterface.

在Python中定义一个可以与NSXPCInterface一起使用的协议是不可能的,因为该类需要"扩展方法签名"。它不能通过在Objective-C运行时以编程方式创建协议的公共API注册。

作为一种解决方法,你必须在一个小的C扩展中定义协议来定义协议。PyObjC文档在https://pyobjc.readthedocs.io/en/latest/notes/using-nsxpcinterface.html上描述了一个小问题,包括如何避免这个问题。

最新更新