views:

898

answers:

6

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.

+1  A: 

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

nall
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 http://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.
Kenny
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.
nall
A: 

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

Kendall Helmstetter Gelner
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.
Kenny
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?
Kendall Helmstetter Gelner
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.
Kenny
+3  A: 

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

miguel.de.icaza
Thanks! I'll try it.
Kenny
+2  A: 

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.

miguel.de.icaza
Great, thanks you!
Kenny
A: 

Staying on the topic of the original question, which appears to be the first and only time this has been addressed on the entire internets, can you generate a library in monotouch that can be statically linked from a Objective C iPhone application?

miguel.de.icaza said: "...you can 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."

excellent. Now, what the heck are you talking about? Could you provide a link or two to the documentation that will get us started? This would be a massive win for every potential dot net centric client I am engaging!

Can you, and if so how, build a library that is consumable from Objective C in XCOde?

levous
A: 

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: h**p://www.mono-project.com/Embedding_Mono Also the Mono-Embedding-API documentation might be helpful: h**p://go-mono.com/docs/monodoc.ashx?tlink=root:/embed

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: h**p://monotouch.net/Documentation/Binding_New_Objective-C_Types You could also use pure C-method calls from the embedding/P/Invoke-API.

Hope this gets you started.

** only one link allowed as new user - thank you stackoverflow (Actually I'm Kenny, the original poster of the question, but I've lost my SO-cookie. Very stupid solution.)

Kenny