Is there a way to mix MonoTouch and Objective-C?

佐手、 提交于 2019-11-27 18:33:53

The setup that you describe is possible, but the pipeline is not as smooth as it is when you do your entire project in MonoTouch. This is in fact how we bootstrapped MonoTouch: we took an existing Objective-C sample and we then replaced the bits one by one with managed code.

We dropped those samples as they bitrot.

But you can still get this done, use the mtouch's --xcode command line option to generate a sample program for you, and then copy the bits that you want from the generated template.m into your main.m. Customize the components that you want, and just start the XCode project from there.

During your development cycle, you will continue to use mtouch --xcode

Kenny

Re: unknown (google):

We actually did this as described.

See this page for a quick start, but the last code segment on that page is wrong, because it's omitting the "--xcode"-parameter. http://monotouch.net/Documentation/XCode

What you have to do to embed your Mono-EXE/DLL into an Objective-C program is to compile your source with SharpDevelop, then run mtouch with these parameters:

/Developer/MonoTouch/usr/bin/mtouch --linksdkonly --xcode=output_dir MyMonoAssembly.exe

This only works with the full version of MonoTouch. The trial does not allow to use the "--xcode"-argument . The "--linksdkonly"-argument is needed if you want mtouch to keep unreferenced classes in the compiled output, otherwise it strips unused code.

Then mtouch compiles your assembly into native ARM-code (file extension .s) and also generates a XCode template which loads the Mono-Runtime and your code inside the XCode/ObjC-program. You can now use this template right away and include your Obj-C-code or extract the runtime loading code from the "main.m"-file and insert it into your existing XCode-project. If you use an existing project you also have to copy all .exe/.dll/.s files from the xcode-output-dir that mtouch made.

Now you have your Mono-Runtime and assembly loaded in an XCode-project. To communicate with your assembly, you have to use the Mono-Embedding-API (not part of MonoTouch, but Mono). These are C-style API calls. For a good introduction see this page.

Also the Mono-Embedding-API documentation might be helpful.

What you have to do now in your Obj-C-code is to make Embedding-API calls. These steps might involve: Get the application domain, get the assembly, get the image of the assembly, locate the class you want to use, instantiate an object from that class, find methods in class, call methods on object, encapsulate method arguments in C-arrays and pass them to the method-call, get and extract method return values. There are examples for this on the embedding-api-doc-page above.

You just have to be careful with memory consumption of your library, as the mono runtime takes some memory as well.

So this is the way from Obj-C to C#. If you want to make calls from C#/Mono into your Obj-C-program, you have to use the MonoTouch-bindings, which are described here.

You could also use pure C-method calls from the embedding/P/Invoke-API.

Hope this gets you started.

Over the weekend it emerged that someone has been porting Cocos2D to .NET, so you could also do the whole work on .NET:

http://github.com/city41/CocosNet

Cocos2D started as a Python project, that later got ported to Objective-C, and now there is an active effort to bring it to C#. It is not finished, but the author is accepting patches and might be a better way forward.

Calling Objective-C from MonoTouch definitely looks possible. See the Objective-C selector examples

What library are you calling? Perhaps there's an Objective-C equivalent.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!