Breaking News

iOS iAd Integration

Ad is used to display ads, which is served by the apple server. iAd helps the user in earning revenue from an iOS application.

iAd Integration – Steps Involved

  • Step 1. We need to create a simple view-based application.
  • Step 2. We need to select your project file, then select targets and then add iAd.framework in choose frameworks.
  • Step 3. We need to Update ViewController.h as follows −
1
#import <UIKit/UIKit.h>
2
#import <iAd/iAd.h>
3
@interface ViewController : UIViewController<ADBannerViewDelegate>
4
{
5
  ADBannerView *bannerView;
6
}
7
@end
8
  • Step 4. We need to update ViewController.m as stated below−

1
#import "ViewController.h"
2
@interface ViewController ()
3
@end
4
@implementation ViewController
5
- (void)viewDidLoad
6
{
7
  [super viewDidLoad];
8
  bannerView = [[ADBannerView alloc]initWithFrame:
9
  CGRectMake(0, 0, 320, 50)];
10
  // Optional to set background color to clear color
11
  [bannerView setBackgroundColor:[UIColor clearColor]];
12
  [self.view addSubview: bannerView];
13
}
14
- (void)didReceiveMemoryWarning
15
{
16
  [super didReceiveMemoryWarning];
17
  // Dispose of any resources that can be recreated.
18
}
19
#pragma mark - AdViewDelegates
20
-(void)bannerView:(ADBannerView *)banner
21
didFailToReceiveAdWithError:(NSError *)error{
22
  NSLog(@"Error loading");
23
}
24
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
25
  NSLog(@"Ad loaded");
26
}
27
-(void)bannerViewWillLoadAd:(ADBannerView *)banner{
28
  NSLog(@"Ad will load");
29
}
30
-(void)bannerViewActionDidFinish:(ADBannerView *)banner{
31
  NSLog(@"Ad did finish");
32
}
33
@end
34

Output

When we execute the application, we’ll get the below result −

 iOS Tutorial