BUILDING THE XAMARIN WEBRTC iOS LIBRARY: USING XAMARIN BINDINGS
In our previous article, we highlighted and explained how to create an Android binding using an AAR library and dealing with errors were applicable using the guidelines for Android specific Xamarin bindings.
In this article, we will be dealing with its iOS counterpart. Straight of the bat, I will state this: iOS bindings as it pertains to WebRTC is decidedly harder and will take more programming/coding time.
There are three key stages to follow to successfully build the iOS WebRTC binding and they are itemized below:
1. OBJECTIVE SHARPIE: API definitions and Enums file creations.
2. Xamarin iOS binding creation and API definition and Enums file substitution.
3. Verification attribute, Error and Bugs fixing and implementation.
Below, I itemized key resources used in for this tutorial:
(S/Nos — — — Resources — — — Description/Link)
- Visual Studio for Mac (2019). Visual Studio for Mac Download
2. Visual Studio Code Visual Studio Code Download
3. Binding Objective-C Libraries Binding Objective-C Libraries
4. Binding Types Reference Binding types reference guide
5. Objective Sharpie Getting Started with Objective Sharpie
6. Explanation on API Definition and Struct file ApiDefinitions & StructAndEnumFiles
7. Verify Attributes Objective Sharpie Verify Attributes
8. Binding iOS Swift Libraries Binding iOS Swift Libraries
You need to use a MacOS computer to run this tutorial successfully. You also need to have Visual Studio 2019 for Mac 2019 installed alongside Objective sharpie. The links to the resources are listed above.
1. OBJECTIVE SHARPIE: API DEFINITIONS AND ENUM FILE CREATION:
We will start of from the result obtained from the building of the native iOS library Xamarin WebRTC -Article 3: a WebRTC.framework folder.
Open the folder in Visual Studio Code and open a terminal making sure to have its current directory as the WebRTC.framework.
Next, we need to check the available sdks. To do this, type the following:
xcodebuild -showsdks
This will list out the available sdks as shown below:
Notice I also did a preliminary check to update my sharpie (Objective Sharpie tool) with the command: sharpie update
Finally, we will create the API definitions and Enum files by executing the following command:
sharpie bind –sdk=iphoneos13.7 –output=”WebRTCApiDef” –namespace=”WebRTC” –scope=”/Users/[username]/[path to folder]/WebRTC.framework/Headers” “/Users/[username]/[path to folder/Headers/WebRTC.h”
Once executed, if you get some errors stating that a certain RTC***.h file could not be found, open your WebRTC.h file in Visual Studio Code:
Perform a Find and Replace all for the following
FIND ALL — — — REPLACE ALL
<WebRTC/ — — — “
> — — — “
Once this is done, you can re-run the previous command and the errors: cannot find RTC**.h file issue should be resolved.
The result of this is the creation of a folder name WebRTCApiDef whose content is composed of two files: ApiDefinitions.cs and StructAndEnum.cs
We will be using the code generated in the two files in the next step.
2. XAMARIN IOS BINDING PROJECT CREATION AND API DEFINITION AND ENUM SUBSTITUTION
Open Visual studio for Mac and Create a new Project: Mac > Library > Bindings Library
This will create the iOS bindings project. Replace the contents of the Visual Studio Mac ApiDefinition.cs and Struct.cs with the Visual Studio Code ApiDefinition.cs and StructAndEnum.cs files, respectively. Once done,
right click on the Native Reference folder in the Solution Explorer and click on “Add Native Reference”:
Navigate to the WebRTC.framework folder and select the WebRTC package to be added to your solution project.
Build your solution and feast your eyes on two thousand plus errors. It’s time for the serious stuff.
3. VERIFICATION ATTRIBUTE, ERROR AND BUG FIXING AND IMPLEMENTATION.
For the Struct.cs file, there are little to no errors in the file. You just need to remove the “using WebRTC;” statement and implement the functions you need from the class called “static class CFunctions”:
I specifically implemented the following static classes
(S/Nos — — — File Name — — — Method (Private/Public))
1. RTCLog.cs RTCSetMinDebugLogLevel/SetMinDebugLogLevel
2. RTCFieldTrials.cs RTCInitFieldTrialDictionary/InitFieldTrialDictionary
Rest of the methods can be implemented in interfaces in the APIDefinition.cs file to gain access to the native code.
With that done, we can now face the APIDefinition.cs file which has the bulk of our Errors and fixes.
Firstly, skimming through your APIDefinition.cs file, you will notice some interfaces and methods have the Verify Attribute, this means you need to manaually inspect and update the interface or methods as appropriate. I mostly did not change the struct of the interfaces and methods flagged with this attribute. I will mention I replaced the return type unsafe byte with an IntPtr in many cases. Based on the hints attached to the Verify Attribute, you can make an educated guess on what to do with the method or interface in question.
If you want a more extensive understanding of how to work through all the errors. I suggest you investigate OBJECTIVE SHARPIE EXAMPLES the resources on this page proved especially useful for me in fixing and implementing the necessary code snippets needed to successfully build the iOS Binding Library.
The completed codebase can be found in this GitHub Repository: Xamarin WebRTC iOS Bindings
If you want to delve further into troubleshooting tips for Xamarin Binding of Objective-C libraries, you can check this link: iOS Binding Troubleshooting