| iOS in Practice
Bear Cahill
Integrating into various systems seems to be a big growth area for technology in general, and, especially, in the mobile world. A growing number of apps let you post to Twitter, the Facebook app can sync with your contacts, and ShareKit will let you integrate with a variety of online options. A growing area for integration development is iAds. In this article based on chapter 10 of iOS in Practice, author Bear Cahill shows how to add iAds to an app.
You may also
be interested in…
|
Advertising is not a new way to make money. It’s not even new to
mobile apps. However, Apple does it differently, since the distributor of your
app is also the advertiser (sort of—they may not be advertising, but they are
the warehouse serving the ads and securing the deals).
In our case, our app isn’t really much for selling since it does
about the same thing as iTunes. And, we don’t have any In-App Purchases to
offer the user. So let’s add ads and see if we can monetize it that way.
You may not be able to sell your app for $.99 but, if you can get
$.10 from a user 20 times, you’ve done better. Let’s look at adding iAds to our
app.
Technique 1: Configuring iTunes for iAds
As with some other special additions to apps (GameCenter, In-App
purchases, Apple Push Notifications), iAds requires some online configuration
in iTunes Connect, but not much.
Problem
We need to configure our app in iTunes Connect to include iAds.
Solution
We only need to log into iTunes Connect and enable iAds for our
app.
Discussion
There’s not much to it but, of course, it’s important. Without
enabling iAds for you app, it won’t get any ads. In the Provisioning Portal,
create a new app id. Then, log into iTunes Connect and click on your app to
manage it. Click on the Set up iAd Network button (see figure
1).
Figure 1 iTunes Connect allows you to set up iAd Network for your app
On the iAds page, select your target audience age and click Enable iAds (see figure 2).
Figure 2 Select your target audience age and enable iAds
in iTunes Connect
You may need to check your contracts area in iTunes Connect to
make sure you have all the necessary agreements in place. This is a good idea
to do periodically anyway. I sometimes get asked by clients why their app is no
longer in the AppStore. The first thing I do is get them to log into iTunes
Connect and see if there’s any new or updated contracts they need to agree to. Now,
that we’ve setup iAds in iTunes Connect, let’s add it to the app.
Technique 2: Adding iAds to an App
Adding iAds to your app is a lot like adding a regular view to
your app. In Interface Builder, there is an ADBannerView you can drag and drop
into your project.
Problem
We need to add iAds to our project and handle it accordingly.
Solution
In the UI editor, we’ll add an ADBannerView to our UI ad. In the
code, we’ll handle the callback methods to hide the ad view when no ad is
loaded.
Discussion
Open the RootViewController.xib file. Since our artwork image is
pretty small, we really don’t need the UIWebView to be so big. Let’s use that
for our ad space. Drag the top of the web view down to leave some room, and
drop an ADBannerView in there (see figure 3).
Figure 3 Dropping an ADBannerView control in our web
view UI
For the code, be sure to add the iAd.Framework and add an outlet
in RootViewController.h for the ADBannerView to be connected with Interface Builder.
Declare that RootViewController
implements ADBannerViewDelegate
in the header also. Also declare a bool flat named hidingAdBanner.
When there’s no ad displaying, we want to hide the ADBannerView
control. A method that hides or shows the ad banner view based on what’s passed
in and the current state will help out here (see listing 1).
Listing 1 Method to hide the AdBanner by moving it
offscreen with animation
-(void)hideAdBanner:(bool)hideIt;
{
if ((hideIt && hidingAdBanner)
|| (!hideIt && !hidingAdBanner))
return; #1
hidingAdBanner = hideIt; #2
[UIView beginAnimations:nil context:nil];
int adHeight = adBanner.frame.size.height;
CGRect r = adBanner.frame;
r.origin.y -= adHeight; #3
adBanner.frame = r;
r = webView.frame;
r.origin.y -= adHeight;
r.size.height += adHeight; #4
webView.frame = r;
[UIView commitAnimations]; #5
}
#1 Not needed
#2 Sets flag
#3 Moves banner
#4 Taller Webview
#5 Animates it
There are a few delegate methods for the ADBannerView. With
Interface Builder, set the delegate of the ADBannerView to be RootViewController
.
There are two callback methods we’re interested in: when the ad loads and when
the ad fails to load.
When the app starts, we can call hideAdBanner
with YES so that the
banner is out of view since there’s no ad loaded. When an ad loads, we can call
it again with NO.
If an ad fails to load, we can call it again with YES (see listing 2).
Listing 2 Call hideAdBanner with YES when ads fail
to load and NO when ads load
- (void)bannerView:(ADBannerView *)banner
didFailToReceiveAdWithError:(NSError *)error
{
[self hideAdBanner:YES];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[self hideAdBanner:NO];
}
In the simulator, the ads will load test ads from the Apple
servers. This is a reliable way to know if the code is working. They sometimes
fail to load, which is helpful for testing (maybe it’s intentional?). When the
app launches in the AppStore, it will load real ads.
Summary
iTunes Connect has report features that allow you to see how
your apps are doing as far as ads’ loading, displaying, user clicks, and revenues.
There also is a lot of good information in the Provisioning Portal, the Apple
Developer site, iOS forums, and many other places. I encourage you to get out
there, see how people are solving problems and sharing solutions, and learn as much
as you can. Also, don’t be afraid to ask questions. iOS developers tend to very
helpful.
Here are some other Manning titles you might be
interested in: