Wednesday, March 9, 2011

iAds Integration


Introduction


iAd allows your application to earn revenue by displaying advertisements to the user. Your application dedicates a portion of its user interface to display banner advertisements and in turn you receive revenue when users view or click those advertisements.
While you are developing your application, iAd sends test advertisements to help you verify your implementation is correct. To receive advertisements from iAd in a release application, you need to select the advertising network option for your application before publishing it.



KeyPoint

iAds are supported on ios 4.0 and above

Steps for iAds integration



1. First of all add iAd.framework as existing framework

2. In your .h file

add the below code,


#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
@interface testViewController : UIViewController < ADBannerViewDelegate>{
id adView;
BOOL adBannerViewIsVisible;
}
@property(nonatomic, retain) IBOutlet id adView;
- (void)fixupAdView;
- (int)getBannerHeight;
- (void)createAdBannerView;
@end
3) In your .m file

add the below code,


@synthesize adView;

4) In your viewDidLoad method, add the below code
[self createAdBannerView];

5) In your .m flee also add the below code,



#pragma mark -
#pragma mark iAdMethods
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (adBannerViewIsVisible) {        
adBannerViewIsVisible = NO;
[self fixupAdView];
}
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (!adBannerViewIsVisible) {                
adBannerViewIsVisible = YES;
[self fixupAdView];
}
}
- (void)createAdBannerView {
Class classAdBannerView = NSClassFromString(@"ADBannerView");
if (classAdBannerView != nil) {
self.adView = [[[classAdBannerView alloc] 
initWithFrame:CGRectZero] autorelease];
[adView setRequiredContentSizeIdentifiers:[NSSet setWithObjects: 
ADBannerContentSizeIdentifier320x50, nil]];
[adView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifier320x50];
[adView setFrame:CGRectMake(0.0, 436.0, 320.0, 50.0)];
[adView setDelegate:self];
[self.view addSubview:adView];        
}
}
- (int)getBannerHeight {
return 50;
}
- (void)fixupAdView {
if (adView != nil) {        
[adView setCurrentContentSizeIdentifier: ADBannerContentSizeIdentifier320x50];
if (adBannerViewIsVisible) {
CGRect adBannerViewFrame = [adView frame];
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = 386;
[adView setFrame:adBannerViewFrame];
} 
else {
[UIView beginAnimations:@"fixupViews" context:nil];
CGRect adBannerViewFrame = [adView frame];
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = 436;
[adView setFrame:adBannerViewFrame];
[UIView commitAnimations];
}
}   
}


No comments:

Post a Comment