Xib-Free iOS App with C# – Part 4 – iAd

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:

    private ADBannerView adBannerView;

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:

    this.adBannerView = new ADBannerView (new Rectangle (0, 386, 320, 50));
    this.View.AddSubview (this.adBannerView);

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:

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}

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

    this.adBannerView.FailedToReceiveAd += (object sender, AdErrorEventArgs e) => {
        Console.WriteLine (e.Error.LocalizedDescription);
    };

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

Advertisements
This entry was posted in iPhone Dev and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s