Wednesday, December 22, 2010

How to integrate iAd into your iPhone app

STEPS

1. add iAd framework to your project

(1) Right click Frameworks: Add\Existing Frameworks…
(2) Select "iAd.framework"

(3) "iAd.framework" has been added to your project


2. add ADBannerView to your view
ADBannerView *iAd = [[ADBannerView alloc] initWithFrame:CGRectMake(x, y, w, h)];
iAd.delegate = self;
[self.view addSubview:iAd];
iAd.hidden = YES;
3. implement ADBannerViewDelegate
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
banner.hidden = FALSE;
NSLog(@"bannerViewDidLoadAd");
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
NSLog(@"bannerViewActionShouldBegin");
return YES;
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner {
NSLog(@"bannerViewActionDidFinish");
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
banner.hidden = TRUE;
NSLog(@"didFailToReceiveAdWithError, error:%d ", [error code]);
}

Why it's important to implement ADBannerViewDelegate?

To detect the status of loading advertisement.
In some situation, ads might not be available.
That's why view controller hide ADBannerView after iAd been created in step 2.
When the advertisement has been successfully loaded (- (void)bannerViewDidLoadAd:(ADBannerView *)banner is called),
the view controller then make ADBannerView visible.
If advertisement is not available, ADBannerView will never show up.
Therefore, user will not see a blank area on ADBannerView.

Here's the image that illustrates ADBannerView is added to a view and advertisement is successfully loaded.


If user touch ADBannerView, a popup view will show up and details of the advertisement will be displayed.
After user close the popup view by touching (X), he will go back to the app directly.


reference:
1. Technical Q&A: Hiding iAd banners when ads are not available
2. iAd programming guide
3. How to integrate iAd into your iPhone app include details about:
- solution for setting target sdk < 4.0
- display iAd according to current orientation
- put iAd in table view

No comments:

Post a Comment