16

I want to embed mono into my iOS application. I do not want to use MonoTouch. I want to embed mono manually like this:

http://www.mono-project.com/Embedding_Mono

I've have done this successfully on windows, using the above guide and various online examples, here's a good Windows one:

https://github.com/inkdev/Embedded-Mono-Sample

However I'm having trouble getting started on iOS. I know it can be done, companies like Unity3d use it to power their game engine tech. I can't work out how to compile and link mono for iOS nor can I find any good instructions to do so. I've not found any help using search engines, they exclusively seem to turn up articles about MonoTouch (Xamarin's own commercial wrapper around embedding mono into iOS).

Here's a few more noteworthy links:

http://www.mono-project.com/Mono:ARM

http://web.archive.org/web/20090106023130/http://mono-project.com./Mono:Iphone

Is there somewhere I can get precompiled libraries and headers for Mono for iOS, so in my C code I can simply link and include?

Could someone provide and example of how to compile mono for iOS ARM CPUs?

MonoTouch provides a great wrapper around all of the iOS Objective C APIs, however you don't necessarily need all of that, as I understand it should be possible to compile and then embed mono yourself and then use pInvoke to call the few native functions you will need.

Any help would be greatly appreciated, ty!

sungiant
  • 3,152
  • 5
  • 32
  • 49
  • 2
    Keep in mind that Mono's JIT-compiler cannot function on iOS, so any assemblies loaded into the runtime will have to have been AOT-compiled elsewhere. – cdhowie Jan 24 '13 at 18:03
  • Very true. I am not hoping to use JIT. – sungiant Jan 24 '13 at 18:06
  • 2
    BTW. If you plan to send this to AppStore, you need to be sure that you don't download any code at runtime (all of code should be packaged in your app). It's a big "No-no" based on Apple guideliens – Victor Ronin Jan 24 '13 at 19:26
  • 1
    Indeed, all CIL dlls will need to be AOT compiled into native code. This is a feature of mono and is exactly what Unity3d uses, when generating XCode projects for iOS, to get round the Apple restrictions. – sungiant Jan 24 '13 at 19:35
  • 2
    I have been working on the same problem myself. I do not need all of the MonoTouch "excess" and only need to be able to call a single library from my iOS app. To do this you will have to cross-compile Mono for ARM7 (or ARM7s) to run on iOS and then you will need to compile an x64 version so you can still run it on the simulator in XCode. Once that is done you will need to link the library to your XCode project and follow the standard way of calling into Mono. Once I get a build script written I will post it as an answer to your question. – Khirok Jan 24 '13 at 21:54
  • 4
    I suggest you also read "When do I need to obtain a license from Xamarin to the Mono Runtime?" here: http://www.mono-project.com/FAQ:_Licensing. – Rolf Bjarne Kvinge Jan 24 '13 at 23:22

2 Answers2

1

While not exactly what you are asking for, iOS 7 has added support for scripting with JavaScript. Many people finding this post may want to use this new capability to achieve what you are attempting. Link

Holly
  • 5,270
  • 1
  • 24
  • 27
  • Yes, this is not directly related to how to embed dotNET into an iOS application, but it was an a method for embedding other code into an iOS application. Five years ago when this was posted, it was a great new capability. – Holly Oct 08 '18 at 12:54
0

As Rolf said, you need to read Mono Licensing document first. Embedding mono on iOS requires special permission from Xamarin.

You need to change several build settings from XCode, and here's what I did.

  1. Open (or Create) iOS project
  2. Add libmonoboehm-2.0.a to your project(You can use another version of .a file)
    1. It's in Library/Frameworks/Mono.framework/Versions/{version}/lib folder
  3. Project -> Build Settings -> Header Search
    1. Add path: /Library/Frameworks/Mono.frameworks/Versions/{version}/include/mono-2.0
  4. Project -> Build Settings -> Library Search
    1. Add path: /Library/Frameworks/Mono.frameworks/Versions/{version}/lib

And, code like this in your iOS application

    #include <mono/jit/jit.h>
    #include <mono/metadata/mono-config.h>
    #include <mono/metadata/assembly.h>
    #include <mono/metadata/environment.h>

// ....

    const char* dllFile = "ABSOLUTE_PATH_TO_DLL_OR_EXE";
    domain = mono_jit_init(dllFile);
    mAssembly = mono_domain_assembly_open(domain, dllFile);
// ... Do whatever you want :)

Please remember. You need to get license from Xamarin when embedding mono on iOS.

Enjoy :)

Joon Hong
  • 1,337
  • 15
  • 23
  • 1
    How can this work? I thought the version of mono in /Library/Frameworks/Mono.frameworks/ was compiled exclusively for OSX not iOS... As Khirok says, I was of the understanding that one needs to cross-compile Mono for ARM7 to generate the correct lib files to reference in the XCode iOS project. – sungiant Oct 17 '13 at 13:51
  • 3
    That *might* work on the iOS simulator (because the JIT can work there) but it **won't** work on iOS devices. Even if re-built for ARM you would run into other issues up to being rejected by the AppStore (because of code generation). – poupou Oct 18 '13 at 12:38
  • @poupou // This does work on the iOS simulator, but I couldn't check it on the real device.(provisioning is expired) And it _could be_ accepted by the AppStore. There are already many games(based on Unity3D, which uses Mono) in the AppStore – Joon Hong Oct 21 '13 at 01:48
  • 1
    Unity3D and Xamarin both uses *ahead of time* AOT (not *just in time*, JIT) compilation. You can **not** build that yourself by doing what you suggested above. – poupou Oct 21 '13 at 12:48
  • This won't work on the real device as @poupou mentioned. You're linking the x86 version of the library. The simulator runs x86 code, but the device does not. – Sam Oct 19 '14 at 12:56