Working on Xamarin ObjC Binding

Dheeraj Kumar Gunti

Reading Time : ( words)

When developing on iOS application, we might need some sort of third party controls which may not be available in Xamarin. With respect to iOS app development Objective-C is the core language which is used to develop applications. Xamarin has provided us a way to utilize Objective-C code and embed them in to our Xamarin iOS application. This concept of integration is called as Binding.

When working with Objective-C classes, we need to create a FAT binary which will be included in to a xamarin binding project which will act as the interceptor of dealing with the logic behind that Objective-C classes.

In Order to create a Xamarin Binding Project, please follow the process shown below.

For example sake we are taking one Objective-C project which we are now creating a binding inorder to use that in our application.

Download or clone the above project.

Open the application in XCode and run the application inorder to see the output.

Now we will use the project to create FAT binary. For that we will create a new project. Create new Cocoa Touch Static Library and Choose the name you want.

For this example I am naming it as CircleMenu

Remove the classes generated by default.

Right click on the Project and choose Add Files. Select the classes from the downloaded project and include them in to our project

Now we have included the classes in to our project.

Click on the Project and You see the below details. Resource Tags, Build Settings, Build Phases, Build Rules. This is the place where we can change the project related and build related settings.

When we are building an iOS application, it should support simulator and the real device. That means we need to support multiple architectures. ARM64, ARMv7, ARMv7S architectures which support real devices. i386 is meant for Simulator.

We need to alter and add the respective architectures on which we need to build the application. Add all those.

As the application should run on the chosen architecture, we need to change the default selection from Yes to No. We are not building on Active archiecture.

When we build the application, respective binaries are stored in machine default location. We need to change that to project location. So that it will be easy to manage the binaries. Go to File Menu and Choose Project Settings as below.

Choose Derived data to Project-Relative Location and click on Done.

Now we need to change the project scheme when building. Click on project dropdown and choose Edit Scheme. You can even create a new scheme or manage existing schemes from here.

Once we have selected Edit Scheme and you will see the below screen. Change the build configuration to release and click on Close.

Now select Product and choose Build For Running. We need to repeat this step for simulator and generic device. Because we need to create FAT binary for both simulator and real device.

Now open terminal and navigate to the project path as below. Project Location > Derived Data > Project > Build > Products

Now you can see two directories Release-iphoneos and Release-iphonesimulator.

User LIPO command find the respective architecture.

lipo -info Release-iphonesimulator/libCircleMenu.a

lipo -info Release-iphoneos/libCircleMenu.a

Now we need to combine these two binries and need to create one binary.

lipo -create Release-iphonesimulator/libCircleMenu.a Release-iphoneos/libCircleMenu.a -output libCircleMenuUnified.a

Now you can see one binary file created on the same directory as below.

Now open Xamarin Studio and create new Xamarin Binding Project as below.

Specify the desired name. For this example I have used CircleMenu.

Now add the FAT Binary .a file in to our project by choosing Add Files.

Now we need to download Objective Sharpie, which is used to create binding classes. Download Objective Sharpie. Once the installation is done. Go to terminal and use the below command to verify which sdks are available in our system.

sharpie xcode -sdks

You can see all the sdks available in the system.

Now run the sharpie command to create binding classes.

sharpie bind -output=CircleMenu -namespace=CircleMenu "Provide the complete path of .h file" -sdk=iphoneos10.1

On executing the command, tool will create ApiDefinitions.cs file. We need to include the contents of that file in to the ApiDefinitions.cs file which already exists in our Xamarin Binding Project.

ApiDefinitions.cs file looks like below.

Build the application. If any error you notice then we need to resolve them one by one. Because Objective Sharpie doesn't guarentee you 100% conversion.

If no errors then the build will be successfull.

Few errors which you can blindly remove.

  • Remove [Verify] attribute.
  • Any duplicate code generated

Once all the process is done. Now you are good to go to add this library to your application.

Enjoy Coding.... Hope this given you a basic idea of how to create Obj-C bindings.