1. Prepare your Python file. Here you can use objective-C clases and etc.
from Foundation import *
import objc
class TestClass (NSObject):
def doit_(self, input):
print input
return "hello from Python"
2. Create a plug-in. Create a setup script like this (replace 'helloPython.py' with your script name):
from distutils.core import setup
import py2app
setup(
plugin = ['helloPython.py']
)
And run the setup script with this command:
python setup.py py2app
You'll get a .plugin folder in dist folder
3. Adds plug-in to your project. Drag the file into Resource folder in your Xcode project. Choose: Create Folder References for any added folders when Xcode ask
4. Load plug-in in objective-C by adding this code (change helloPython to your .plugin name)
NSString *pluginPath = [[NSBundle mainBundle]
pathForResource:@"helloPython"
ofType:@"plugin"];
NSBundle *pluginBundle = [NSBundle bundleWithPath:pluginPath];
[pluginBundle load];
4. Creating a python-class object in Objective-C
Class TestClass = NSClassFromString(@"TestClass");
id pyObj = [[TestClass init] alloc];
NSString *result = [pyObj doit:@"sent from obj-c"];
NSLog(result);
0 comments:
Post a Comment