Thursday, 13 March 2014

iPhone Development: Integrate iAd in IOS (Tutorial)





iAd example
iAd example
Ads == $$ revenue, most of the average time spent by a user on a smartphone is dedicated to app interaction rather then making a call or reading sms. So that's increase chance for app developer to get revenue by showing ads in their apps.

Their are so many third party ads SDK, Apple also provided ads with their own SDK. They bring iAd framework to Xcode that allows user to add ad functionality to their app and generate revenue. In this post, i am going to tell you how can we use iAd framework in our iOS apps.


Add iAd.framework to your project.Import to your header file in which you want to implement ads.Set its delegates and create an instance of AdBannerView.

@interface TMAdView : UIViewController
{
    ADBannerView *ad;
}


Now select your .m file and in its viewDidLoad, allocate and initialise your ADBannerView instance.Give it frame(position in your screen )where you want to place ads.Set its delegate to self, as we need to adjust our layout if there is no ad(if we did not adjust our layout based on ad load and unload, there will be blank space if no ad is available for that region)and finally add it to our view as a subview.

        ad = [[ADBannerView alloc]init];
        ad.frame=CGRectMake(0,0, 320, 50);
        ad.delegate=self;
        [self.view addSubview:ad];



Now. it's time to implement our delegates, all our self explanatory as per their name and also line written in NSLog.

-(void)bannerView:(ADBannerView *)banner
didFailToReceiveAdWithError:(NSError *)error{
    NSLog(@"Error in Loading Banner!");
    banner.hidden=YES;
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner

{
     NSLog(@"iAd banner Loaded Successfully!");
}


-(void)bannerViewWillLoadAd:(ADBannerView *)banner

{
    NSLog(@"iAd Banner will load!");
    banner.hidden=YES;
}


-(void)bannerViewActionDidFinish:(ADBannerView *)banner

{
  NSLog(@"iAd Banner did finish");
 }


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

No comments:
Write comments