Xamarin and Chartboost SDK iOS Bindings
When developing a cross platform app (PCL) using Xamarin I needed to integrate with the Chartboost SDK. I spent a lot of time searching and could not find any bindings projects and most of the references on the usual forums where years out of date.
So I took the plunge and decided to have a go at building it myself, I read the official documents from Xamarin and they are helpful but still felt a bit out of date and also never had a walkthrough of using an iOS .framework SDK.
So I took the plunge and decided to have a go at building it myself, I read the official documents from Xamarin and they are helpful but still felt a bit out of date and also never had a walkthrough of using an iOS .framework SDK.
- The first step which all article agree on was to download Sharpie from https://developer.xamarin.com/guides/cross-platform/macios/binding/objective-sharpie/ this utility from Xamarin helps create the C# bindings for the library file.
- Fire up Xamarin and Create a New Solution and choose to create an iOS Library > Bindings Library
- Download the latest SDK from Chartboost (at the time of writing mine is v6.6.3)
- Expand the Chartboost .zip and navigate to Chartboost-iOS-6.6.3/Chartboost.framework/Versions/Current folder
- In that folder there is a file called Chartboost (its around 50Mb)
- You need to rename this file to Chartboost.a (this is a key step I never saw mentioned in the docs)
- Drag the Chartboost.a file onto your Xamarin project and it will create a Chartboost.linkwith.cs file and show it under the Chartboost.a
- Now you are ready to run Sharpie, so open a Terminal and change the current directory to the Xamarin project folder
- Run this command to find out what the latest SDK version you have is sharpie xcode –sdks and look for a line like sdk: iphoneos10.3 arch: arm64 armv7 you will need the sdk name on the next command
- The command I used was as follows, notice the long path, I am telling Sharpie where the chartboost.framework file is relative to the Xamarin project folder sharpie bind -framework ../../../Chartboost-iOS-6.6.3/Chartboost.framework -sdk iphoneos10.3 -namespace Chartboost
- You will now have a completed ApiDefinition.cs and Structs.cs file with the Chartboost API. Sometimes I found Sharpie aded an extra but similar named files, if this happens, open up the new files and copy over to the ApiDefinition.cs and Structs.cs files
- Open the Structs.cs file and replace the nuint with ulong as the type
- There a number of Verify compiler directives, which I just removed from both the ApiDefinition.cs and Structs.cs files
You now have a valid project that you can compile and make a binding library file for use in other Xamarin projects. The next step is how to get the Chartboost library working in a PCL project.
There are 3 steps, first you need to define a delegate to pass into the Chartboost library which can respond to the events being raised. The key thing I found here was to not call the CacheInterstitial until the DidInitialize event fires. I lost some time figuring this out as in a native iOS app I’ve never had to do that:
There are 3 steps, first you need to define a delegate to pass into the Chartboost library which can respond to the events being raised. The key thing I found here was to not call the CacheInterstitial until the DidInitialize event fires. I lost some time figuring this out as in a native iOS app I’ve never had to do that:
public class CBDel : ChartboostDelegate
{
public override void DidCacheInterstitial(string location)
{
Debug.WriteLine("DidCacheInterstitial:" + location);
}
public override void DidFailToLoadInterstitial(string location, CBLoadError error)
{
Debug.WriteLine("DidFailToLoadInterstitial" + error.ToString());
}
public override void DidInitialize(bool status)
{
Debug.WriteLine("DidInitialize" + status.ToString());
Chartboost.SetShouldPrefetchVideoContent(true);
Chartboost.CacheInterstitial( Constants.CBLocationHomeScreen);
}
}
{
public override void DidCacheInterstitial(string location)
{
Debug.WriteLine("DidCacheInterstitial:" + location);
}
public override void DidFailToLoadInterstitial(string location, CBLoadError error)
{
Debug.WriteLine("DidFailToLoadInterstitial" + error.ToString());
}
public override void DidInitialize(bool status)
{
Debug.WriteLine("DidInitialize" + status.ToString());
Chartboost.SetShouldPrefetchVideoContent(true);
Chartboost.CacheInterstitial( Constants.CBLocationHomeScreen);
}
}
Now you need to initialise the Chartboost library, so define an instance of your delegate to pass into the initialise:
public static CBDel del = new CBDel();
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
Chartboost.StartWithAppId("APPID HERE", "APP SECRET HERE", del);
Chartboost.SetShouldPrefetchVideoContent(true);
return true;
}
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
Chartboost.StartWithAppId("APPID HERE", "APP SECRET HERE", del);
Chartboost.SetShouldPrefetchVideoContent(true);
return true;
}
Finally here is the code in a simple button to show the interstitial:
partial void UIButton3_TouchUpInside(UIButton sender)
{
bool ret = Chartboost.HasInterstitial( Constants.CBLocationHomeScreen);
if (ret)
Chartboost.ShowInterstitial( Constants.CBLocationHomeScreen);
else
Chartboost.CacheInterstitial( Constants.CBLocationHomeScreen);
}
{
bool ret = Chartboost.HasInterstitial( Constants.CBLocationHomeScreen);
if (ret)
Chartboost.ShowInterstitial( Constants.CBLocationHomeScreen);
else
Chartboost.CacheInterstitial( Constants.CBLocationHomeScreen);
}