Hoang Quach
5 min readFeb 28, 2020

How to use Chilkat library in Xamarin.Forms (iOS/Android)

If you want use Chilkat in the Xamarin.Forms project? Unfortunately, it’s not something that Chilkat explicitly supports at this time. Check back in the future to see if this is still the case.

Workaround: Binding native library to *.dll can use on the XF project :)

For iOS:

  1. Download latest version of “Chilkat iOS Objective-C / Swift / C / C++” from https://www.chilkatsoft.com/downloads_ios.asp
    — Download and unzip to any directory
    — Chilkat provides static libs for each architecture slice: i386, x86_64 (for the simulator), armv7, armv7s, arm64, and armv6 (for older versions of iOS).
    — The Objective-C headers are contained in the “include” directory.
    — The C/C++ class headers are contained in the “cpp_include” directory.
The unzipped directory structure is shown here

2. Use libtool to “Building an iOS Universal Chilkat Static Library”. One for Simulator and One for Device. Please don’t mix them
— Open terminal and go to unzip folder in the step 1
+ Build for Simulator: Run command

/usr/bin/libtool -static i386/libchilkatIos.a x86_64/libchilkatIos.a -o libchilkatIos.a

+ Build for Device: Run command

/usr/bin/libtool -static armv7s/libchilkatIos.a armv7/libchilkatIos.a arm64/libchilkatIos.a -o libchilkatIos.a

3. Open your project on VS, click solution and create new project iOS >> Library >> Bindings Library (C#)
— Create one binding project for Simulator and one for Device
— All steps bellow you need to do two times for each projects.

4. Copy file “Static Library” from Step 2 “libchilkatIos.a” into binding project. — Open VSMac and add exits file.
— VS detect and auto generate a file with the name “libchilkatIos.linkwith.cs

— Open it and add new parameter, “LinkerFlag” with value “-lresolv -lpthread -lstdc++

5. This step is really really important
— The binding project consists of the static library we just created and metadata in the form of C# code that explains how the Objective-C API can be used. This metadata is commonly referred to as the API definitions.
— So in this our case, chilkat is a huge library, if you need use all functions of chilkat lib. Humzz, that fucking job :)) because you need define all functions into the ApiDefinition.cs. We have a tool “Objective Sharpie” can auto convert and generate all it for you, but you need review a result and fixing (Objective Sharpie does a great job of helping us, but it can’t do everything.)
— So in my opinion, you just only define which feature you use.

6. Testing case → Use Crypt/Decrypt feature
— Example on native (Object-C AES Encryption): https://www.example-code.com/objc/crypt2_aes.asp

— Let reviewing example above, we need define in ApiDefinition.cs.

  • Class “CkoCrypt2” with properties “CryptAlgorithm,CipherMode,KeyLength,PaddingScheme,EncodingMode
  • Class “CkoCrypt2” with functions “SetEncodedIV,SetEncodedKey,EncryptStringENC,DecryptStringENC

— Back to Step 1, open directory of native lib, open “include” dir and open file “CkoCrypt2.h
+ Find the fucking line define all properties and functions above
+ Look and make a define for each.

Define class
Define property
Define function

— If you need to use a lot of features, you can try with “Objective Sharpie” to auto generate ApiDefinition.cs. In this demo case with “CkoCrypt2.h”

https://docs.microsoft.com/en-us/xamarin/cross-platform/macios/binding/objective-sharpie/get-started

Objective Sharpie is a command line tool provided by Xamarin that assists in creating the definitions required to bind a 3rd party Objective-C library to C#. Even when using Objective Sharpie, the developer will need to modify the generated files after Objective Sharpie finishes to address any issues that could not be automatically handled by the tool.

  • Open terminal, go to unzip directory in Step 1 and try with the following command:
  • Before we can start the binding process, we need to get information about our current installed SDKs by entering the following command into the Terminal sharpie xcode -sdks:
  • Try process CkoCrypt2.h with command: sharpie bind — output=Cko2Crypt2 — namespace=ChilkatBinding — sdk=iphoneos13.2 include/CkoCrypt2.h
  • Open folder CkoCrypt2 and look the file ApiDefinition.cs. Its cool ha :))
28K lines of code :))
  • You should copy the define of properties and functions you need to use into ApiDefinition.cs in project binding (Xamarin Project)

7. Add a reference from iOS project to binding project, make a build, and you can use Chilkat for iOS :)

For Android:

  1. Download latest version of “Chilkat for Android™ Java” from https://www.chilkatsoft.com/chilkatAndroid.asp
    — Download and unzip to any directory
    — Chilkat provides libs for each architecture slice: armeabi, mips, and mips64
The unzipped directory structure is shown here

2. You need compress all java class in the folder com.chilkatsoft into a single Jar file. You can use Elipse IDE or Android Studio or Terminal with Java SDK to do this job. Or download it from my source

3. Open your project on VS, click solution and create new project Android >> Library >> Bindings Library (C#)

4. Copy single Jar library from Step 2 in to binding project. Add exits file into Jars folder and set Build Action ==> Embedded Jar

5. Copy libs folder in Step 1 into Android project and rename to “jniLibs”. Add exits folder into Android project and set Build Action ==> Android Native Library for each file

6. Open MainActivity.cs and add this line to load chilkat lib.
Java.Lang.JavaSystem.LoadLibrary(“chilkat”);

7. Add a reference from Android project to binding project at Step 3, make a build, and you can use Chilkat for Android :)

And create dependency service for use chilkat in view model

Source: https://github.com/bulubuloa/ChilkatBindingXF