When creating a bindings library for a Xamarin.iOS project, the approach is different and we have to use Visual Studio for Mac and create a new project of typeBinding Library (see Picture 6).
A new empty iOS Bindings Library (Picture 7) consists of a Native References folder, under which the original third party library in format “[Packagename].framework” should be placed under the Resources folder for possible image / file / font resources and the most important ApiDefinitions.cs file. This file is empty now, but will contain C# interfaces for all objects in the originalnative library.
After the empty project is created, proceed to the following steps to prepare iOS bindings library:
1) Download the native library to your Mac
2) Check SDKs installed on your Mac
3) Download and install the ObjectiveSharpie tool (see reference [8])
4) Run the Objective Sharpie command
5) Include the .framework folder and replace the ApiDefinitions.cs file in the project by the one created in the previous step
6) Correct build errors generated by the compiler by modifying the ApiDefinitions.cs file
Now let’s look more closely at each step.
Find the library you want to use in your Xamarin.iOS app online and download to anyfolder. Go to the [Packagename].framework folder. It should contain “Header”and “Modules” subdirectories along with other library files.
Open the terminal and run the following command:
{% c-block language="xml" %}
xcodebuild -showsdks
{% c-block-end %}
You should get output like this:
On your Mac, download and install a very useful tool – Sharpie. Using this tool, an ApiDefinitions.cs file is created for the native library header .h file. I suppose it’s almost impossible to create a real iOS bindings library without Sharpie.
So go to https://aka.ms/objective-sharpie and install the .pkg package on your Mac.
Open the terminal and go to the folder with the downloaded native library in step 1).
Run the sharpie command like this one:
{% c-block language="xml" %}
sharpie bind-sdk iphoneos14.5 ./Usercentrics.framework/Headers/Usercentrics.h -namespaceUsercentrics -scope Usercentrics.framework/Headers -c -F
{% c-block-end %}
As a result, two files are generated – ApiDefinitions.cs and StructsAndEnums.cs.
Open the iOS binding library solution in Visual Studio for Mac, and by right-clicking on the “Native References” folder choose “Add Native Reference”. Select the .framework folder and confirm.
Now when trying to build the project, some build errors always appear. Based on the complexity of the binded native library, the ApiDefinitions.cs file may contain thousands or hundreds of thousands of lines. It’s out of the scope of this article to deal with any of errors which may occur. But I recommend that you read this great post here [9].
After dealing with all build and native linker errors, you’ll get the final .dll library, which can be included in a Xamarin.iOS project as a reference. All APIsdefined in ApiDefinitions.cs can be now used.
Any iOS bindings library created by the above process uses the original library as an ObjectiveC static library (.a file). This causes a problem when trying to use it in an iOS app generated by Microsoft HotRestart [10] built and deployed on a Windows machine. You’ll get a runtime error in app parts which use the third party api.
Read previous: Xamaring Bindings Libraries, an Android part (1/3)
Read next: Xamaring Bindings Libraries, working example (3/3)