3

I want to use OpenFeint in a MonoTouch project. I have no experience in creating the proper bindings to use a third party Objective-C library with Mono. Reading this article on the Xamarin website did not really help me any further. Has anyone created the bindings necessary to use OpenFeint with MonoTouch or has anyone experience with creating the bindings I need?

Thijs
  • 2,341
  • 2
  • 14
  • 22

2 Answers2

3

It is really easy.

File->New Project->MonoTouch->MonoTouch Binding project

First thing add the Objective-C library (*.a file), it's Build option should automatically say Native Library.

Add all the header files they provide with a Build option of None (this is just for reference to help you write the binding).

Read through the header files and start writing the binding in the ApiDefinitions.cs file:

  • Use [BaseType(typeof(NSObject))] unless their class inherits from something else
  • Use [Export("yourMethod:")] on methods
  • Notice the + and - symbols on methods, + indicates a static method, use [Static] on your end to indicate that
  • If you come across a delegate class (one you need to inherit), add [Model], otherwise it will come out as a sealed class
  • If you need to link other libraries to compile, modify the [LinkWith] attribute in the designer.cs file that shows up underneath the *.a library
  • Enumerations go in the other *.cs file (I forget the name)
  • #define MyConstant 1 - constants like these should go in the class who's header file they're in. Define a new *.cs file with the class as partial. You can also put extra C# code on the class if you wish
  • Map NSTypes to the appropriate C# type: NSString -> string, etc.
  • Feel free to rename the Obj-C types so they aren't as dumb. I've run across random prefixes on every method, member, etc.--remove stuff like that.
  • READ the link you supplied in your question
  • When all is said and done, just reference the new library (don't use any extra build options in project settings, you don't have to use that any more)

In general, it is a good idea to just do it yourself so you are comfortable binding Obj-C libraries like a boss. That is what I would tell a new hire in my department.

jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
  • I've downloaded the OpenFeint SDK and am able to build it using xcode. Unfortunately there isn't a *.a file anywhere on my HDD. Any pointers on how to obtain that? – Thijs Mar 26 '12 at 12:02
  • I see a `BuildUniversalBinary.sh` file in the download. Can you run that and see what it does? I am on Windows right now, I can't. – jonathanpeppers Mar 26 '12 at 12:09
  • I am trying to run the file but it is giving me the following error: builduniversalbinary.sh: line 6: CONFIGURATION: unbound variable. I am trying to figure out how to fix it. Let me just confirm that I am supposed to use the file as follows in Terminal: "sh BuildUniversalBinary.sh" – Thijs Mar 26 '12 at 12:45
  • You can also just try `.\BuildUniversalBinary.sh`, you may have to run `chmod 755 BuildUniversalBinary.sh` first. – jonathanpeppers Mar 26 '12 at 12:57
  • I keep getting the configuration unbound variable error. I've read the instructions that came with the SDK, performed all the steps. I can build the SDK using xcode but the BuildUniversalBinary.sh keeps throwing that error. So, since I do have a 23MB file called OpenFeint I figured I would try adding the extension .a and see where that takes me... keep you posted! – Thijs Mar 27 '12 at 06:26
  • It might be the right file, I noticed it in the download but thought it odd that there was no extension. – jonathanpeppers Mar 27 '12 at 13:31
1

In addition to @Jonathan excellent answers...

There are several bindings projects available on github, e.g. from Xamarin, that can give you hints if you're unsure how to convert some Objective-C constructs into C#. Real examples are often very helpful along the theory.

If you ever get blocked in a particular place then do not hesitate to ask specific questions, either here or on the mailing-list.

poupou
  • 43,413
  • 6
  • 77
  • 174
  • Thanks, great suggestion! I would up vote both answers but alas, I don't have the necessary reputation. – Thijs Mar 27 '12 at 06:28