Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Xamarin.iOS Binding for an Existing iOS Objective-C Library with Objective Sharpie

DZone's Guide to

Xamarin.iOS Binding for an Existing iOS Objective-C Library with Objective Sharpie

Vidyasagar Machupalli takes us step-by-step through generating a Xamarin.iOS binding for an existing iOS Objective-C library with Objective Sharpie.

Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

These days a new phase has crept into my developer life and I call it "generating Xamarin.iOS binding for Objective-C libraries". After tasting success by generating Xamarin C# binding of a Cocoapods iOS SDK using Objective Sharpie, a new request hoped-in from the MobileFirst Platform Foundation team.

This time,the task is to generate Xamarin.iOS binding for Bluemix Push Notification Service Client SDK-Push Notification and engagement services that you will love. As I am well versed with binding creation, I thought it would be a cakewalk. Following the same steps as mentioned in the blog post, I ended up generating a definition binding for:

 IBMMobileFirstPlatformFoundation (Dependent class)against IBMMobileFirstPlatformFoundationPush.

After a bit of research, here's how we achieved our goal.

Just a quick note before we start: Objective Sharpie is a command line tool to help bootstrap the first pass of a binding. It works by parsing the header files of a native library to map the public API into the binding definition (a process that is otherwise done manually). Objective Sharpie does not create a binding by itself, but it can help get you started!

Step 1:

Install and Setup Objective Sharpie. Download here.

Tip: Use the sharpie Update command to Update to the Latest Version.

For any assistance, while using Objective Sharpie, use sharpie -help which shows you all the commands and options available.

Vidyasagars-MacBook-Pro:~ VMac$ sharpie -help
usage: sharpie [OPTIONS] TOOL [TOOL_OPTIONS]

Options:
  -h, -help                Show detailed help
  -v, -version             Show version information

Telemetry Options:
  -tlm-about               Show a detailed overview of what usage and binding
                             information will be submitted to Xamarin by
                             default when using Objective Sharpie.
  -tlm-do-not-submit       Do not submit any usage or binding information to
                             Xamarin. Run 'sharpie -tml-about' for more
                             information.
  -tlm-do-not-identify     Do not submit Xamarin account information when
                             submitting usage or binding information to Xamarin
                             for analysis. Binding attempts and usage data will
                             be submitted anonymously if this option is
                             specified.

Available Tools:
  xcode              Get information about Xcode installations and available SDKs.
  pod                Create a Xamarin C# binding to Objective-C CocoaPods
  bind               Create a Xamarin C# binding to Objective-C APIs
  update             Update to the latest release of Objective Sharpie
  verify-docs        Show cross reference documentation for [Verify] attributes
  docs               Open the Objective Sharpie online documentation

Step 2:

Open Terminal and run the below command:

sharpie xcode -sdks

sdk: appletvos9.2    arch: arm64   
sdk: iphoneos9.3     arch: arm64   armv7   
sdk: iphoneos9.2     arch: arm64   armv7   
sdk: macosx10.11     arch: x86_64  i386    
sdk: watchos2.2      arch: armv7k 

Step 3:

For this step, we need a static library with all the required.h and.m files. If you don't have one, follow the instructions here to create.

With an assumption that all the required header files (.h) including the Main Header file (in our case IBMMobileFirstPlatformFoundationPush.h) are sitting in the same folder, Run the below command on Terminal with appropriate output, namespace, SDK information based on your expected output.

sharpie bind --output=IBMMobileFirstPlatformFoundationPush --namespace=IBMMobileFirstPlatformFoundationPush --sdk=iphoneos9.2  [full-path-to-STATIC LIBRARY FOLDER on your machine]

Note: In my case, I can use iOS 9.2 or iOS 9.3 As a parameter for --sdk. Please use the appropriate SDK version based on the output.

You should see the below files under the folder where you are running all the above commands.

Definition Files for IBMMobileFirstPlatformFoundationPush

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:
xamarin ,xamarin.ios ,push notification ,push notifications ,ibm mobilefirst ,mobilefirst ,c# ,objective c

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}