Add Google Ads to your Xojo app
You may have a little iOS app made with Xojo, that shows something interesting to customers. That may be a nice little puzzle game. You give it away for free, but play an ad between levels.
To start, you sign up for Google Ad service on developers.google.com/ad-manager/mobile-ads-sdk/ios/quick-start. Please read there. You skip the installation for the framework as we embed all the code within our plugin.
For the info.plist, you need the entries for a few things. First you get the application identifier for your application. You may use our info.plist sample file and change it. Please keep the SKAdNetworkItems entries there (maybe update them to newer list?). You may want to keep the NSUserTrackingUsageDescription if you like to allow the framework to ask for tracking and you get higher ad prices. Since iOS applications need to declare via info.plist what they load, we use NSAppTransportSecurity entry to just inform iOS, that the ads may be loaded from anywhere.
In the iOS project we add a new class GADMobileAds with super class GADMobileAdsMBS. Having a subclass allows you later to add events there. We add a module with a property sharedInstance As GADMobileAds, so we store that at a central place. Could also be attached to the app class or a screen. Next in the Opening event of the first screen we initialize it and call the start method:
sharedInstance = New GADMobileAds
If you like you can add the startCompleted event in the GADMobileAds class, so you know when the startup in background is ready. You may use that to set a flag, so you can later see if you can query an ad.
Now let's say, we like to show an ad between levels. You add an ad unit on the google website for your application and give it a name. Then you pick up the ad id for your source code. Google will collect the metadata about the ad behavior in that ad unit and later show you the statistics, so you may want to make different ids for different areas of the application, where you show ads.
Now you make a new request, optionally setting some options. But usually we take the default one for later:
Dim request As GADRequestMBS = GADRequestMBS.request
We add a new cldass GADInterstitialAd to the project with super class GADInterstitialAdMBS. Add a property fullscreenAd for such an object of the class and then initialize with the ad id:
fullscreenAd = New GADInterstitialAd
fullscreenAd.loadWithAdUnitID AdId, Request
You may need to load the ad a few seconds before you like to show it, so it can load. Add the loadCompleted event to the GADInterstitialAd class. Set a flag when the ad is ready. Then when you switch levels, you can call the add with calling:
// show ad
Once this runs, the ad shows and your app basically waits. For example you show the ad over the screen to start the next level. You may add events like DidRecordClick and DidRecordImpression, so you know when the impression or a click is recognized and you get paid.
Please check our example project and play with it. You may ad more events and other ad types to make some extra income for your application. Let us know if you have questions.