Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

iOS integrates iAD


May 21, 2021 iOS Development Manual


Table of contents


IOS iAD integration


Brief introduction

IAD is Apple's advertising platform, which helps developers earn revenue from apps.

The instance step

1. Create a simple View base application

2. Select the project file, then select the target, then select the framework and add iAd.framework.

3. Update ViewController.h as follows

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
@interface ViewController : UIViewController<ADBannerViewDelegate>
{
    ADBannerView *bannerView;
}
@end

4. Update the ViewController .m, as shown below

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    bannerView = [[ADBannerView alloc]initWithFrame:
    CGRectMake(0, 0, 320, 50)];
    // Optional to set background color to clear color
    [bannerView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview: bannerView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - AdViewDelegates

-(void)bannerView:(ADBannerView *)banner 
 didFailToReceiveAdWithError:(NSError *)error{
    NSLog(@"Error loading");
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
    NSLog(@"Ad loaded");
}
-(void)bannerViewWillLoadAd:(ADBannerView *)banner{
    NSLog(@"Ad will load");
}
-(void)bannerViewActionDidFinish:(ADBannerView *)banner{
    NSLog(@"Ad did finish");

}
@end

Output

Run the application and get the following output:

iOS integrates iAD