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.
- Convert .h file into .cs file.
- Download and install a command line tool called Objective Sharpie
- Open Terminal
- Type Command: sharpie bind -output Binding -sdk iphoneos8.1 Path of your .h file
- iphoneos8.1 will be your Xamarin Studios’s iOS target version
- Hit enter and you get ApiDefinitions.cs and StructEnum.cs in your home directory.
- 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.
- 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”.
- Create a binding project in Xamarin Studio.
- Under resources directory add the .a file and it will eventually create libSomething.linkwith.cs.
- In libSomething.linkwith.cs you will have to define the frameworks it needs while executing.
- Replace the code of the ApiDefinitions.cs and StructEnum.cs which you just converted.
- Set ForceLoad and SmartLink to “true”.
- 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).
- 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!
- 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 🙂