Search code examples
iphonepythonobjective-cpyobjc

Calling custom Objective-C from a pyobjc application?


This question is basically the inverse of this other question: Calling Python from Objective-C

I have implemented my iPhone application logic in Objective-C (obviously), and am now trying to re-use as much as possible from my XCode project in the server component to save on double-implementation. I have successfully loaded the CoreData data model from Python, however, can't see a way to actually call into the Objective-C logic from Python.

Basically I'm trying to access the Objective-C classes and methods in my iPhone project from Python to save myself duping out all the implementations.

Is this even vaguely possible, or is dupe-implementation the only solution here? Seems like the kind of thing Boost::Python might be used for, but I'm not really sure.

edit: Boost::Python won't work because it is C++ based and I need Objective-C. I knew there was a reason why that didn't work.


Solution

  • If your Objective-C code is in a framework and you would like to essentially write a Python application that uses your framework, then you can use objc.loadBundle, and then use objc.lookUpClass or NSClassFromString to get access to your classes. From there, you can use your classes like any other bridged Objective-C class.

    If you're running your Python code within a process that already has the Objective-C runtime up, and your classes are already registered with it, then you can skip the loadBundle step.