Xamarin Bindings Libraries, an iOS part (2/3)

Xamarin Bindings Libraries, an iOS part (2/3)
February 7, 2022
In the previous part, we learned how to create a bindings library for Android. Now let’s focus on the iOS version. For an iOS library, a developer needs a Mac. There are requirements which make it impossible to accomplish this part on a Windows machine.

1. iOS Bindings Library

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.

1.1. Download the native library to your Mac

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.

 

1.2. Check the SDKs installed on your Mac

Open the terminal and run the following command:

{% c-block language="xml" %}
xcodebuild -showsdks
{% c-block-end %}

You should get output like this:

1.3. Download and install the Objective Sharpie tool

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.

 

1.4. Run the Objective Sharpie command

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.


1.5. Include the .framework folder and replace ApiDefinitions.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.

 

1.6. Fix build errors generated by the compiler by modifying the ApiDefinitions.cs file

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.

 

1.7. Attention! iOS static library

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.

Picture 6 – New Bindings Library project dialog in Visual Studio for Mac
Picture 7 – Empty Bindings Library project in Visual Studio for Mac

 

Read previous: Xamaring Bindings Libraries, an Android part (1/3)

Read next: Xamaring Bindings Libraries, working example (3/3)

Share:
Luboš is an experienced .NET developer (web and mobile apps) very familiar with SQL and DevOps. He developed a lot of web applications used worldwide by industry and the academical world. He holds a bachelor’s degree in applied physics and astrophysics. Likes bowling, rides e-mountainbike, skis and plays the violin.

Article collaborators

SABO Newsletter icon

SABO NEWSLETTER

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

About SABO Mobile IT

We focus on developing specialized software for our customers in the automotive, supplier, medical and high-tech industries in Germany and other European countries. We connect systems, data and users and generate added value for our customers with products that are intuitive to use.
Learn more about sabo