In April of this year, during the presentation of the new iOS 4 operating system (the former iPhone OS), Steve Jobs also introduced the new Apple platform for mobile advertising - 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 a combination of interactivity, which is provided by the use of a smartphone or tablet, with the emotionality of an advertisement, in which both audio and video are involved. For these purposes, creating advertisements uses Java Script, HTML5, CSS3 and multitouch. All this allows you to create interesting and eye-catching commercials applications. Examples of such advertisements we could
see 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 - moving, more often, to the advertiser's site. With iAd, we can get around this trouble. After clicking on the banner advertisement appears on top of your application. The user can close it at any time and return to your application at the exact moment in which he left it.
')
Based on the iAd Programming Guide and the video from WWDC 2010, I sketched a brief overview-translation of this new technology. It will not be about creating colorful advertisements themselves, but about placing those in your applications.
The first thing you need to do to place an ad in your application is to add to your Xcode project
iAd.framework . It is contained in the iOS 4 SDK. Well, do not forget to add #import <iAd / iAD.h> where necessary. Developers are offered to use two banners to choose from - 320 x 50 pixels. for portrait layout and 480 x 32 pix. for landscape. The basis of this banner is the
ADBannerView class, which is a subclass of UIView. Therefore, all you need is to embed this view into your hierarchy of interface controls. You can do this both programmatically and through Interface Builder - a new UI element 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 the impressions (and as a result your money), and the user is uncomfortable 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. Change the code in iAdExViewController.h
#import <UIKit/UIKit.h> #import <iAd/iAd.h> @interface iAdExViewController : UIViewController "<"ADBannerViewDelegate">"
and in iAdExViewController.m we change the viewDidLoad method
- (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 dwell a little bit more on the properties
requiredContentSizeIdentifiers and
currentContentSizeIdentifier .
In the first property, you specify all kinds of banners that you are going to use in your application. And the second property is what type of banner you will use at this particular moment. Since I am still writing the simplest application, we’ll continue with the same 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 discuss. The device receives advertising banners from the network. What happens if we are in a place where there is no connection? Or some interruptions with Apple c server advertising? Then our ADBannerView will be empty. Not only is it not beautiful, it also takes the place of our application in vain. Apple strongly advises to use, as they call it, “the ideal realization” - when in the absence of advertising for some reason the banner is removed from the application screen, and when the connection is restored and an advertisement is received - it is again displayed on the screen. We knowingly attributed
ADBannerViewDelegate in the description of our class, and now it can receive messages from the banner. In case of successful ad acceptance, the banner will send us a message
bannerViewDidLoadAd , and in case of any problems with this,
didFailToReceiveAdWithError message. We now implement the required methods 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; } }
Run

Click on the banner

So we implemented everything correctly, as comrades from Apple ask us. If we now launch the application, we will see that there is a banner, it shows a test ad, and if we click on it, then a large test advertisement will open. But there is one problem. It's good to me that I have a test application that does nothing at all, except as a banner shows. And you, probably, have games and music players, and other interactive applications. If you do the same thing as me, you will notice that your application will continue to play music or video while the ad is showing and it turns out to be a mess. And if you have a game and you need to urgently respond to the event - then users generally will shy away from advertising in your application. In order to solve these issues, we will implement the
bannerViewActionShouldBegin methods in our application, when a large advertisement starts to unfold and
bannerViewActionDidFinis h, when minimized.
- (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]; // }
Separately dwell on
BOOL shouldExecuteAction . I indicated - YES. You, too, should try as often as possible to say YES to your ADBannerView. Thus, you allow him to turn around full screen after the click. If you have an important process in the application, and you do not want to allow this process to be interrupted, then you can reply to this message - NO and the advertisement will not open. However, Apple asks us very much whether or not to do this at all or only in emergency cases. Think yourself - there is a banner - the user clicks on it - but there is no advertising. Disorder! As the iAd direction manager at WWDC said: “You can answer NO, but I can’t even imagine what the situation should be.”
All that is left of the "ryushechek" we still have to do is change the banner depending on the orientation of the phone. If you remember, during initialization, we specified the requested banners for portrait orientation only. Therefore, we need to correct this line now.
- (void)viewDidLoad {
...
adView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifier320x50,ADBannerContentSizeIdentifier480x32,nil];
...
}
and this is how the following methods will look like
- (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; }
The result after turning the simulator:

That is, when you change the orientation of the phone, we change the
currentContentSizeIdentifier property of our ADBannerView. Now we can watch advertising banners in our application and in landscape orientation. In the screenshot “in the album” it turned out not very nice because of the shifting by 50 pixels of the banner - but this is already you correct yourself.
More about a couple of small things that were mentioned at WWDC. Apple engineers are urging you to write a line for the object ADBannerView .delegate = nil; Very, very much asked. In our particular example, this would look like:
- (void)dealloc { adView.delegate=nil; [adView release]; [super dealloc]; }
We are done with the program part. We made a banner, made it work in two orientations, learned to handle the events and errors of this banner.
Now briefly about the payment - to get money for the reflection of advertising from the iAd Network - you will need another contract with Apple, which you can create as standard through the developer portal. In the process of setting up your application on the portal, you will answer the questions: “Is the target audience of your application under 17 years old?” And set up words and URL exceptions — ads with which words you would not want to show. For example, you would not want to see your competitors in your application. Well, then - wait for the arrival of your check with money.
The work of iAd is expected to begin on July 1 - so roll up your sleeves and append applications - there is still time. While on the developer forum there is information that test ads appear unstable. Indeed, while I was writing an article today, ads appeared at the launch of the application, then did not appear. But everything has its charms - but I also tested the method of the appearance of a banner, when advertising was finally received from the network.