Adventures in iOS Native Bindings

Published on

I was attempting to implement Rewarded Interstitial Ads in my Avalonia AdMob project, and found my assumption that Android would be the more difficult platform couldn't be more wrong...

In this article, I go over some of the things I learned while updating native iOS bindings for the AdMob SDK.

The Problem

Jc.AdMob.Avalonia.iOS has a dependency on Xamarin.Google.iOS.MobileAds, Xamarin (and net6.0ios) bindings for the Google Mobile Ads SDK.

This worked great for the most part. However, I wanted to implement rewarded interstitial ads and the problems began. Ironically I started with Android, thinking it would be the hardest to implement, but the process there was relatively painless.

When I got to iOS, I found that I was getting app crashes and a runtime exception:

Objective-C exception thrown. Name: NSInvalidArgumentException Reason:
-\[GADRewardedInterstitialAd presentFromRootViewController:delegate:\]: unrecognized selector sent to instance 0x600002a7c260

The Why

So, what was going on? The error is pretty clear, it's telling me GADRewardedInterstitialAd presentFromRootViewController:delegate: is an unrecognized selector.

Checking the GADRewardedInterstitialAd documentation, I see the signature should be presentFromRootViewController:userDidEarnRewardHandler:.

And checking the Xamarin binding project, I see the selector is indeed incorrect.

The Solution

Now here's where things get interesting...

So I had a few options, and I tried them all...

  • Use Objective Sharpie to attempt to generate net8.0 bindings
  • Write a Swift wrapper library around the AdMob SDK
  • Fork the bindings repo, update the selector and move on

tl;dr I went with the latter, but as the original binding repo is read only and no longer maintained, I took what I needed and am publishing a Jc.Xamaring.Google.iOS.MobileAds binding package on NuGet.

Objective Sharpie

I tried using Objective Sharpie to generate the bindings, but I was getting a lot of errors and warnings. It's possible the AdMob SDK is just too complex for Objective Sharpie to handle, or it does not provide enough context for Sharpie to generate what is required, I'm not entirely sure.

I think if I give myself more time, I'd like to further explore this method and generate some net8.0 bindings as net6.0 is no longer supported by Microsoft.

Swift Wrapper

I tried giving this a go, but I'm not very familiar with Swift and I quickly came to the realization that this would be a lot of work, a lot of time and potentially not even work after having to write a significant amount of Swift to get basic functionality off the ground.

I think this may be a good option in the future if I cannot get more modern binding generator solutions to work with AdMob. But will be very time consuming, time I don't currently have.

Fork the Repo

The options I ultimately went with, for now, was to download the repository code and strip out everything I did not need, make the fix and publish a package with the updated bindings.

The Future

While the current solution works, it's not ideal. I'd like to revisit this again at some point in the future when I have more time to get the bindings in net8.0 to avoid net6.0 out of support warnings/errors. Moving away from old Xamarin binding generators and towards the modern MAUI approach would be far more ideal too (there is probably a reason why the original repo is now read-only and no longer maintained).