Skip to content

Instantly share code, notes, and snippets.

@kevinmcmahon
Created March 19, 2011 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinmcmahon/877696 to your computer and use it in GitHub Desktop.
Save kevinmcmahon/877696 to your computer and use it in GitHub Desktop.
MonoTouch Binding Help
NOTE: This is pulled from an email exchange and hopefully will be polished up and published as a blog post at http://blog.kevfoo.com but until then I'll just dump it here in a gist. If you have any questions leave comments or ask them in #monotouch on IRC. (I am klmcmahon on IRC)
<Begin Excerpt>
I'm going to do a quick brain dump and attach some code for a generator that was passed around on the #monotouch IRC channel. This is extremely raw and stream of conscious but probably what will become the blog post when it gets fleshed out better.
Step 1: You _HAVE_ to have a static library that has been compiled without THUMB code being generated. (NOTE: I haven't upgraded to iOS 4.3 tools and XCode yet but apparently this isn't the case anymore since Apple appears to have fixed the bug) There is a linker bug that Apple introduced and has yet to fix that causes problems at compile time when trying to link in static libraries with THUMB enabled. You can run a command via the shell to check the dumped output (here is an example : http://monotouch.pastebin.com/4Kq52f9D Look at line 5, second column. The two byte address is the give away that the library is THUMB) but I don't have the command off the top of my head. The best way to handle this is to get the full Objective-C source and uncheck the THUMB code option. You also might want to look into making a fat static library. I do this for most of the bindings I publish so that there is only one static library to link against and you don't have to do hassle with two libs (one for the sim which is i386 and one for the device which is typically an armv6/armv7 library).
Step 2: You need to create the C# bindings. This is where the MonoTouch docs at http://monotouch.net/Documentation/Binding_New_Objective-C_Types start to come into play. There is a tool you can compile from the source found in this gist (https://gist.github.com/512356) that can help save some time. Build the tool and run it by doing (without quotes) "mono ObjCParser.exe <FILENAME>.h" passing the header files associated with the ObjC lib you're trying to bind. You can pass multiple header files. You'll get three .cs files generated from that which contain the C# bindings you can use. NOTE: the files actually use the MonoMac namespaces etc as it was built to assist the binding effort there.
The tool isn't perfect and there are known issues. Cleaning this tool up is on my todo list but extremely low since I know the quirks and it is just faster for me to gen and handle it than fix and polish it. The three biggest ones that I see a lot are 1) if a method specified in a .h file spans multiple lines the tool can't recognize that. When you go to compile you'll get some errors. 2) NSURLRequest in ObjC is NSUrlRequest in MT or something to that effect. There are a few places where the ObjC classes don't map exactly into MT classes due to naming convention issues. 3) You'll need to update and figure out how to handle the delegates. Due to the brute force nature of the generator tool, the delegates are typed as IntPtr's when in reality they should be typed so it requires so hand updating to straighten the delegates out.
Step 3: generate the bindings by invoking btouch on the files. The invocation looks something like this:
/Developer/MonoTouch/usr/bin/btouch -out=<YOURNAMEHERE>.dll <YOUR_BINDING_CODE>.cs
Step 4: include a reference to the newly generated dll in your MT project. You will also need to include the static library in the project. For some reason it isn't enough to just link to the path. You actually need to have the file included in the .csproj file. You can set the build action to None.
Step 5: update the additional arguments. They go in the Extra Arguments field in the iPhone Build settings under the Project Options. Screengrab here: http://grab.by/8PtI
You can read more in the official docs under the "Linking the Dependencies" section of
http://monotouch.net/Documentation/Binding_New_Objective-C_Types
These are the basic steps. It isn't hard but it isn't exactly straight forward. My best recommendation is to take a look at my fork of the Localytics project on github here: https://github.com/kevinmcmahon/monotouch-localytics and try and reproduce the bindings on your own.
Hope this helps and can tide you over until I get something published.
Kevin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment