This is Part 4 of our series building a xib-free iOS app with C#.

In our last episode, we displayed and updated the current time based on an NSTimer. We experimented with timer periodicity and finally settled on using a cross-platform timer from System.Threading. Today, we will add an iAd banner to the app. Apple has made this really easy.

Xib-Free iAd Banner

Add an iAd banner view to the CurrentWeatherClockViewController:

[code lang=text] private ADBannerView adBannerView; [/code]

As we’ve done before, right-click ADBannerView and select Resolve > using MonoTouch.iAd to add the iAd framework.

Add the following code at the end of the ViewDidLoad method to create the ad banner and add it to the view:

[code lang=text] this.adBannerView = new ADBannerView (new Rectangle (0, 386, 320, 50)); this.View.AddSubview (this.adBannerView); [/code]

Build and run. Wait a couple minutes and you’ll see ads. You can tap the ad to see some Apple promotional information that demonstrates some of the iAd capabilities.

If you examine the Application Output window, you will see errors like this:

[code lang=text] ADBannerView: Unhandled error (no delegate or delegate does not implement didFailToReceiveAdWithError:): Error Domain=ADErrorDomain Code=3 “The operation couldn’t be completed. Ad inventory unavailable” UserInfo=0xf8af800 {ADInternalErrorCode=3, ADInternalErrorDomain=ADErrorDomain, NSLocalizedFailureReason=Ad inventory unavailable} [/code]

Implement the missing event handler by adding this code to the end of ViewDidLoad.

[code lang=text] this.adBannerView.FailedToReceiveAd += (object sender, AdErrorEventArgs e) => { Console.WriteLine (e.Error.LocalizedDescription); }; [/code]

If you’ve done this in Objective-C, you’ll appreciate how easy that was!