iAd framework was introduced in iOS 4.0. The framework does most of the things for you, such as downloading ads from remote network. You just need to integrate with this framework and place ads on your iPhone/iPad app.
Advertisements in iAd are available in
Banner view : Ads are displayed on a portion of device screen.
Full screen view : Occupies the whole screen during display.
User can interact with the ads by clicking on them and the app owner earns revenue when user views or interacts with the ad.
You should agree to iAd agreement on iTunes before integrating iAd services into your app. Also, iAd has to be explicitly enabled for each app before submitting the app to appstore.
Let's see how to integrate a Banner type ad into an app. I am using Navigation/Master-Detail ViewController app for this post.
1) Include iAd framework in your app.
2) Declare BannerViewContainer protocol as shown below in iAdIntegrationAppDelegate.h (assuming name of the app to be iAdIntegration). Add/Modify the highlighted part of code.
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
@protocol BannerViewContainer
- (void)showBannerView:(ADBannerView *)bannerView animated:(BOOL)animated;
- (void)hideBannerView:(ADBannerView *)bannerView animated:(BOOL)animated;
@end
Make sure that iAdIntegrationAppDelegate confirms to ADBannerViewDelegate. Also, declare an object of AdBannerView class.
@interface iAdIntegrationAppDelegate : NSObject <UINavigationControllerDelegate, UIApplicationDelegate,ADBannerViewDelegate> {
UIWindow *window;
UINavigationController *navigationController;
ADBannerView *ibannerView;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end
3) Add/modify the highlighted part of code in iAdIntegrationAppDelegate.m file
Declare an object of UIViewController class to hold the current ViewController object.
UIViewController *icurrentController;
Instantiate ibannerView and make the current object as its delegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ADBannerView *tempbannerView = [[ADBannerView alloc] initWithFrame:CGRectMake(0.0, 367.0, 0.0, 0.0)];
ibannerView = [tempbannerView retain];
[tempbannerView release];
ibannerView.delegate = self;
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
icurrentController = nil;
return YES;
}
Implement methods of ADBannerViewDelegate
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[icurrentController showBannerView:banner animated:YES];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[icurrentController hideBannerView:ibannerView animated:YES];
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
//perform activities after the ad is clicked and before action is triggered.
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
//perform activities after the app interface is restored.
}
Get the viewcontroller which confirms to BannerViewContainer protocol and call showBannerView method if banner is correctly loaded.(viewcontroller on which you want to display ads)
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
icurrentController = [viewController respondsToSelector:@selector(showBannerView:animated:)] ? (UIViewController *)viewController : nil;
if (ibannerView.bannerLoaded && (icurrentController != nil)) {
[(UIViewController *)viewController showBannerView:ibannerView animated:NO];
}
}
4) Now implement methods of BannerViewContainer protocol in the viewcontroller where you want to display ads.
Add/Modify the highlighted part of code in FirstViewController.h. Make sure FirstViewController confirms to BannerViewContainer protocol.
@interface FirstViewController : UIViewController<BannerViewContainer>
Add/Modify the highlighted part of code in FirstViewController.m. Implement the methods of BannerViewContainer protocol.
ADBannerView *_bannerView;
- (void)showBannerView:(ADBannerView *)bannerView animated:(BOOL)animated
{
_bannerView = bannerView;
[self.view addSubview:_bannerView];
}
- (void)hideBannerView:(ADBannerView *)bannerView animated:(BOOL)animated
{
_bannerView = nil;
}
For more info visit
No comments:
Post a Comment