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

5

24

I'd like to know if there is a way to mix C# and Obj-C code in one project. Specifically, I'd like to use Cocos2D for my UI in Obj-C and call some MonoTouch C#-Library that does some computations and get some values back. Is there a way to do this? Or maybe the other way around, i. e. building in MonoTouch and calling Cocos2D-functions? Thanks.

Therine answered 5/10, 2009 at 20:37 Comment(0)
D
15

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

Dolly answered 10/10, 2009 at 17:26 Comment(2)
Perhaps this is no longer the best way: --xcode=DIRECTORY ... Note that this option is not supported, and may be removed in a future version of mtouch.. I believe that Apple relaxed restrictions on interpreted languages a while ago, such that if you include all code that will be interpreted with your package it is okay. This would lead me to conclude that embedding Mono and calling it from Objective-C/Objective-C++ would be the way to go.Jubilant
Miguel: you might want to update your post to indicate that this is no longer possible.Fascism
T
10

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.

Therine answered 17/1, 2010 at 14:32 Comment(0)
D
6

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.

Dolly answered 19/10, 2009 at 19:45 Comment(1)
This is a deprecated project and has not been worked on for several years. Look into cocos2d-xUnexceptional
E
2

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

Exhibitionist answered 5/10, 2009 at 22:16 Comment(2)
Yeah, I saw that page too, but that's too complicated. It'd be best to have it as described above with GUI in Cocos3D and MonoTouch-Library. Looked at monotouch.net/Documentation/XCode where embedding is mentioned and also a link to the main Mono framework, but don't know if this is the way to go.Therine
Well, it sounds like embedding the Mono VM is the way to go, since it acts like a library to your primary application. The directions on that Xcode page seem pretty straightforward.Exhibitionist
O
1

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

Omniumgatherum answered 5/10, 2009 at 22:36 Comment(3)
It's a custom C# library, which is difficult to port. It already compiles with MonoTouch, but I need a way to incorporate this with the UI, which is in Cocos3D.Therine
I didn't say to port it. I was asking what it does, because there's a good chance you can find another library that does what you want to do, in Objective-C (or even C). So, what does the library DO?Omniumgatherum
You're right, you didn't say to port it. But I don't think there is an Obj-C/C equivalent, since that library is based on recent research of a colleague. In a nutshell it's some kind of reasoning engine. I think we'll try Miguel's proposal.Therine

© 2022 - 2024 — McMap. All rights reserved.