Binding Objective C into Xamarin

Our urge to use a Native library in MonoTouch environment is so much, yet we face difficulty in doing so on weekly if not daily basis. I personally found Xamarin developer documents are not so straightforward and clear in understanding. In this binding process our ultimate goal is to convert “Cocoa Touch Library” or “Cocoa Touch Framework” to “.dll”. The process of binding of “Cocoa Touch Library” and “Cocoa Touch Framework” are little different.

Binding Objective C library :

Before we start you please create a folder and keep the fat or universal library (.a file) and all the corresponding header files. If you are unaware of Fat Library then check out my article.  

  1. Convert .h file into .cs file.
    1. Download and install a command line tool called Objective Sharpie
    2. Open Terminal
    3. Type Command:  sharpie bind -output Binding -sdk iphoneos8.1  Path of your .h file    
    4. iphoneos8.1 will be your Xamarin Studios’s iOS target version
    5. Hit enter and you get ApiDefinitions.cs and StructEnum.cs in your home directory.
    6. ApiDefinitions.cs file contains the C# version of the methods, protocols, properties etc. of the .h files. There may be multiple .h files you have but after conversion it keep all the classes inside the same file.
    7. StructEnum.cs as the name suggests it contains the “struct” “enum” which was part of .h files.

 

Tips (you can skip): You can convert .h file manually also, without using “Objective Sharpie Tool” but it’s a tedious process, you have to convert each and every line of the header file. We generally don’t have so much time, do you? Even if you use “Objective Sharpie Tool” you still have to fix some compiler errors but it will be very less. If Sharpie tool will not be 100% sure about the conversion then it  puts a “Verify” tag on top the line. For instance when it converts @protocol to Interface, it starts its name with “I” and it looks like ISomeDelegate and it gives error, which simply can be resolved by removing “I”.

 

  1. Create a binding project in Xamarin Studio.
    1. Under resources directory add the .a file and it will eventually create libSomething.linkwith.cs.
    2. In libSomething.linkwith.cs you will have to define the frameworks it needs while executing.
    3. Replace the code of the ApiDefinitions.cs and StructEnum.cs which you just converted.
    4. Set ForceLoad and SmartLink to “true”.
    5. Compile the binding project and you will get .dll inside the Bin folder of the project.

Binding Objective C Framework:

Cocoa touch framework contains “.exec” file along with header files (.h file).

  1. Just change the name of .exec file eg. SampleFramework.exec to libSampleFramework.a and follow the same process as we did for Objective C libraries. That it!
  2. If the framework has a .bundle, copy this to your project (not the bindings project), be aware not to the binding project! It needs to be added to resource folder of your solution this is because resources in Xamarin.iOS are not supported in DLLs.

 

I had spent so much time in figuring out about binding that I ended up writing this short and crisp article to help fellow developers. If this doesn’t solve your problem reach me out I will definitely try to help! I may convert and give it you for few bucks 🙂

 

Advertisements

One thought on “Binding Objective C into Xamarin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s