0

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.
Durgaprasad
  • 1,910
  • 2
  • 25
  • 44
  • PyObjC author here. It looks like NSXPCInterface requires information that PyObjC currently doesn’t provide. I’d have to investigate what’s going on here, and if it is possible to provide the extended information from PyObjC. – Ronald Oussoren Aug 20 '21 at 15:07
  • A workaround is to define the protocol in a small C extension that’s compiled using clang. Not very nice, but at least you could continue. – Ronald Oussoren Aug 20 '21 at 15:07

1 Answers1

1

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.

Ronald Oussoren
  • 2,715
  • 20
  • 29