Tag Archives: Google Ads

Issue in the Xamarin.iOS binding library for Google Ads

This week, I had to work about an issue around the integration of Google Ads into a Xamarin.iOS application.

To integrate Google Ads in your application, Xamarin has published a binding library above the official iOS SDK (written in Objective-C). This library is available as a NuGet package.

After adding the package in your project, it’s very easy to use and in just few lines of code, you can create a search request and retrieve ads matching your criteria. The sample code below shows you how to achieve that (it has been simplified for brevity).

var request = new SearchRequest();
request.Query = "Xamarin Mobile Development";

var banner = new SearchBannerView(...); 
banner.LoadRequest(request);

Unfortunately, with the actual version of the NuGet package, this code won’t compile. The error is on line with the LoadRequest statement.

After some investigations, I’ve discovered the source of the problem. If you look at the official iOS SDK, you can see declarations like this :

@interface GADRequest : NSObject<NSCopying>
@interface GADSearchRequest : GADRequest

The GADSearchRequest class (aka. interface in the Objective-C terminology) inherits from the GADRequest class.

But if you look the source code of the binding library available on GitHub since few weeks (Xamarin has announced at Evolve 2016 that a lot of components are now open-source), you can see these declarations :

[BaseType(typeof(NSObject), Name="GADRequest")]
interface Request : INSCopying

[BaseType(typeof(NSObject), Name="GADSearchRequest")]
interface SearchRequest

As you can see, there is a difference compared to the official SDK, because the BaseType of the SearchRequest class is equal to NSObject rather than Request.

I’ve downloaded, modified and recompiled the source code with the following modification.

[BaseType(typeof(NSObject), Name="GADRequest")]
interface Request : INSCopying

[BaseType(typeof(Request), Name="GADSearchRequest")]
interface SearchRequest

With this simple modification, the LoadRequest statement is good and now, I’m able to compile and test the application successfully.

This issue has been submitted on GitHub and the pull request is waiting for approval.

In conclusion, if you encounter some compilation errors like this one (in this library or any other), take a look to the official SDKs, and compare the declarations with the binding library to ensure that everything is good.

Advertisements