Adding iAd Banners to Your iPhone Apps
In April this year, during the presentation of the new operating system iOS 4 (formerly iPhone OS), Steve Jobs also introduced the new Apple mobile advertising platform - iAd. According to Apple, their main task is to give developers the opportunity to earn free and low-cost applications by placing in the latest advertising banners. Payment will go both for displaying an advertising banner and for clicks on it. In the second case, the payment will be significantly higher.
The main idea of iAd is to combine the interactivity that the use of a smartphone or tablet gives with the emotionality of the advertisement, in which both audio and video are involved. For these purposes, when creating ads, Java Script, HTML5, CSS3 and multi-touch are used. All this allows you to create interesting and eye-catching commercials-applications. We could see examples of such advertisements at the presentation of the new operating system (in English) .
The main problem of mobile advertising today, according to Apple, is that when a user clicks on an advertising banner, the user leaves the application - most often, they go to the advertiser's website. With the help of iAd we can get around this trouble. After clicking on the banner, an advertisement appears on top of your application. The user can close it at any time and return to your application exactly at the moment in which he left it.
Based on the iAd Programming Guide and the video from WWDC 2010, I outlined a brief overview-translation of this new technology. It’s not about creating colorful advertisements themselves, but rather about placing them in your applications.
The first thing to do to advertise in your application is to add to your project in Xcode - iAd.framework. It is contained in the iOS 4 SDK. Well, do not forget to add #importwhere to go. Developers are offered to use two banners to choose from - 320 x 50 pixels. for portrait layout and 480 x 32 pixels. for landscape. The basis of this banner is the ADBannerView class , which is a subclass of UIView. Therefore, all you need to do is embed this view in your hierarchy of interface controls. You can do this both programmatically and through Interface Builder - a new UI element has appeared there - Ad BannerView.
Apple strongly recommends placing the banner on top or bottom of the screen and not placing it on the moving parts of elements such as ScrollView and TableView - this reduces impressions (and as a result your money), and it’s inconvenient for the user to catch such a banner.

Let's just create a new project in Xcode based on the View-based Application template and add an ad banner to it. Our application will be called iAdEx. We change the code in iAdExViewController.h and in iAdExViewController.m we change the viewDidLoad method. Let us dwell a little more on the properties requiredContentSizeIdentifiers and currentContentSizeIdentifier . In the first property, you specify all kinds of banners that you intend to use in your application. And the second property is what type of banner you will use at this particular moment. Since I am writing the simplest application so far, we will continue with one banner size and portrait orientation, and below I will make the banner change depending on the orientation of the phone.
Let's stop for a second and reason. The device receives banner ads from the network. What happens if we are in a place where there is no connection? Or some kind of interruption in Apple's ad server? Then our ADBannerView will be empty. Not only is it not beautiful, it also takes the place of our application in vain. Apple strongly recommends using, as they call it, an “ideal implementation” - when, for some reason, the banner is removed from the application screen in the absence of advertising, and when communication is restored and the advertisement is received, it appears again on the screen. We knowingly attributed ADBannerViewDelegate in the description of our class, and now it can receive messages from the banner. If the ad is successfully accepted, the banner will send us the message bannerViewDidLoadAd, and in case of any problems with this - the message didFailToReceiveAdWithError . We immediately implement the methods required for this in our application.
We

start We click on a banner

So we implemented everything correctly, as the comrades from Apple are asking us. If we launch the application now, we will see that there is a banner, it shows a test advertisement, and if we click on it, a large test advertisement will open. But there is one problem. It’s good for me that I have a test application that does nothing at all, except for showing a banner. And you probably have games and music players, and other interactive applications. If you do the same as me, you will notice that your application will continue to play music or video while the ad is showing, and you get a mess. And if you have a game and need to urgently respond to an event, then users will generally shy away from advertising in your application. In order to solve these issues, we implement bannerViewActionShouldBegin methods in our applicationwhen a large ad begins to unfold and bannerViewActionDidFinis h when collapsed. Let's
dwell on BOOL shouldExecuteAction.. I indicated - YES. You should also try to say YES to your ADBannerView as often as possible. Thus, you allow him to expand to full screen after a click. If you have some important process in the application, and you do not want to allow interrupting this process, then you can reply to this message - NO and the advertisement will not open. However, Apple very strongly asks us not to do this at all or to do it only in case of emergency. Think for yourself - there is a banner - the user clicks on it - but there is no advertising. Mess! As the iAd referral manager at WWDC said: “You can answer NO, but I have no idea what the situation should be.”
All that remains for us to do from the "ruffles" is to change the banner depending on the orientation of the phone. If you remember, during initialization, we specified the requested banners only for portrait orientation. Therefore, we need to correct this line now, and this is how the following methods will look like. Result after turning the simulator: That is, when the phone orientation changes, we change the currentContentSizeIdentifier property of our ADBannerView. Now we can watch advertising banners in our application and in landscape orientation. The screenshot “in the album” didn’t work out very well due to the 50-pixel shift of the banner - but you’ll fix it yourself.

A couple of little things that were talked about at WWDC. Apple engineers urge you to write the line object before releasing an ADBannerView object .delegate = nil; Very, very asking. In our specific example, it will look:
We are done with the software part. We made a banner, made it work in two orientations, learned how to handle the events and errors of this banner.
Now, briefly about the payment - to receive money for the reflection of advertising from the iAd Network - you will need one more contract with Apple, which you can create as a standard through the developer's portal. In the process of setting up your application on the portal, you will answer the questions: “Is the target audience of your application younger than 17 years old?” And configure the words and URL-exceptions - with which words you would not like to display ads. For example, you would not want to see your competitors in your application. Well, then - wait for the arrival of your check with money.
IAd is expected to start July 1 - so roll up your sleeves and append applications - there’s still time. So far, the developer forum has information that test ads appear unstable. Indeed, while I was writing an article today, announcements either appeared when the application was launched, or did not appear. But everything has its charms - but I also tested the method for the appearance of the banner when the advertisement was finally received from the network.
The main idea of iAd is to combine the interactivity that the use of a smartphone or tablet gives with the emotionality of the advertisement, in which both audio and video are involved. For these purposes, when creating ads, Java Script, HTML5, CSS3 and multi-touch are used. All this allows you to create interesting and eye-catching commercials-applications. We could see examples of such advertisements at the presentation of the new operating system (in English) .
The main problem of mobile advertising today, according to Apple, is that when a user clicks on an advertising banner, the user leaves the application - most often, they go to the advertiser's website. With the help of iAd we can get around this trouble. After clicking on the banner, an advertisement appears on top of your application. The user can close it at any time and return to your application exactly at the moment in which he left it.
Based on the iAd Programming Guide and the video from WWDC 2010, I outlined a brief overview-translation of this new technology. It’s not about creating colorful advertisements themselves, but rather about placing them in your applications.
The first thing to do to advertise in your application is to add to your project in Xcode - iAd.framework. It is contained in the iOS 4 SDK. Well, do not forget to add #import

Let's just create a new project in Xcode based on the View-based Application template and add an ad banner to it. Our application will be called iAdEx. We change the code in iAdExViewController.h and in iAdExViewController.m we change the viewDidLoad method. Let us dwell a little more on the properties requiredContentSizeIdentifiers and currentContentSizeIdentifier . In the first property, you specify all kinds of banners that you intend to use in your application. And the second property is what type of banner you will use at this particular moment. Since I am writing the simplest application so far, we will continue with one banner size and portrait orientation, and below I will make the banner change depending on the orientation of the phone.
#import
#import
@interface iAdExViewController : UIViewController "<"ADBannerViewDelegate">" //кавычки пришлось поставить, иначе Хабр съедал ADBannerViewDelegate
{
ADBannerView *adView;
BOOL bannerIsVisible;
}
@property (nonatomic,assign) BOOL bannerIsVisible;
@end
- (void)viewDidLoad {
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50);
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
[self.view addSubview:adView];
adView.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];
}
Let's stop for a second and reason. The device receives banner ads from the network. What happens if we are in a place where there is no connection? Or some kind of interruption in Apple's ad server? Then our ADBannerView will be empty. Not only is it not beautiful, it also takes the place of our application in vain. Apple strongly recommends using, as they call it, an “ideal implementation” - when, for some reason, the banner is removed from the application screen in the absence of advertising, and when communication is restored and the advertisement is received, it appears again on the screen. We knowingly attributed ADBannerViewDelegate in the description of our class, and now it can receive messages from the banner. If the ad is successfully accepted, the banner will send us the message bannerViewDidLoadAd, and in case of any problems with this - the message didFailToReceiveAdWithError . We immediately implement the methods required for this in our application.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:@"animateAdBannerOn" context:NULL];
// баннер сейчас не виден и сдвинут за экран на 50 пикселей
banner.frame = CGRectOffset(banner.frame, 0, 50);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
// баннер сейчас виден, и мы его убираем, так как что-то не заладилось со связью
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}We

start We click on a banner

So we implemented everything correctly, as the comrades from Apple are asking us. If we launch the application now, we will see that there is a banner, it shows a test advertisement, and if we click on it, a large test advertisement will open. But there is one problem. It’s good for me that I have a test application that does nothing at all, except for showing a banner. And you probably have games and music players, and other interactive applications. If you do the same as me, you will notice that your application will continue to play music or video while the ad is showing, and you get a mess. And if you have a game and need to urgently respond to an event, then users will generally shy away from advertising in your application. In order to solve these issues, we implement bannerViewActionShouldBegin methods in our applicationwhen a large ad begins to unfold and bannerViewActionDidFinis h when collapsed. Let's
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
NSLog(@"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES; // будете ли вы отвечать на это сообщение баннеру
if (!willLeave && shouldExecuteAction)
{
// останавливаем в нашем приложении всю интерактивность
// [video pause];
// [audio pause];
}
return shouldExecuteAction;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
// возобновите здесь все, что вы остановили
// [video resume];
// [audio resume];
// итд итп
}dwell on BOOL shouldExecuteAction.. I indicated - YES. You should also try to say YES to your ADBannerView as often as possible. Thus, you allow him to expand to full screen after a click. If you have some important process in the application, and you do not want to allow interrupting this process, then you can reply to this message - NO and the advertisement will not open. However, Apple very strongly asks us not to do this at all or to do it only in case of emergency. Think for yourself - there is a banner - the user clicks on it - but there is no advertising. Mess! As the iAd referral manager at WWDC said: “You can answer NO, but I have no idea what the situation should be.”
All that remains for us to do from the "ruffles" is to change the banner depending on the orientation of the phone. If you remember, during initialization, we specified the requested banners only for portrait orientation. Therefore, we need to correct this line now, and this is how the following methods will look like. Result after turning the simulator: That is, when the phone orientation changes, we change the currentContentSizeIdentifier property of our ADBannerView. Now we can watch advertising banners in our application and in landscape orientation. The screenshot “in the album” didn’t work out very well due to the 50-pixel shift of the banner - but you’ll fix it yourself.
- (void)viewDidLoad {
...
adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifier320x50,ADBannerContentSizeIdentifier480x32,nil];
...
}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait|UIInterfaceOrientationPortrait);
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
else
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
}
A couple of little things that were talked about at WWDC. Apple engineers urge you to write the line object before releasing an ADBannerView object .delegate = nil; Very, very asking. In our specific example, it will look:
- (void)dealloc {
adView.delegate=nil;
[adView release];
[super dealloc];
}We are done with the software part. We made a banner, made it work in two orientations, learned how to handle the events and errors of this banner.
Now, briefly about the payment - to receive money for the reflection of advertising from the iAd Network - you will need one more contract with Apple, which you can create as a standard through the developer's portal. In the process of setting up your application on the portal, you will answer the questions: “Is the target audience of your application younger than 17 years old?” And configure the words and URL-exceptions - with which words you would not like to display ads. For example, you would not want to see your competitors in your application. Well, then - wait for the arrival of your check with money.
IAd is expected to start July 1 - so roll up your sleeves and append applications - there’s still time. So far, the developer forum has information that test ads appear unstable. Indeed, while I was writing an article today, announcements either appeared when the application was launched, or did not appear. But everything has its charms - but I also tested the method for the appearance of the banner when the advertisement was finally received from the network.