Durgaprasad
Durgaprasad

Reputation: 1951

PyObjc: implement NSXPCInterface

I am writing an Extension and main app is in PyObjc. I want to setup communication between main app and Extension. With ref to link I tried writing a Protocol.

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 object is created.

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

Registered Metadata as well.

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'}, 
          },
       },
    }
  }
})

But while creating an Interface getting error.

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.

Upvotes: 0

Views: 153

Answers (1)

Ronald Oussoren
Ronald Oussoren

Reputation: 2810

It is not possible to define a protocol in Python that can be used with NSXPCInterface because that class needs "extended method signatures" which cannot be registered using the public API for programmatically creating protocols in the Objective-C runtime.

As a workaround you have to define the protocol in a small C extension that defines to protocol. The PyObjC documentation describes a small gotcha for that at https://pyobjc.readthedocs.io/en/latest/notes/using-nsxpcinterface.html, including how to avoid that problem.

Upvotes: 1

Related Questions