How to Implement IAd in iPhone

Introduction

"iAd"
is a mobile advertising platform developed by Apple for its iPhone, iPad or iPod. These mobile devices allow third-party developers to directly embed advertisements into their applications.

In this article I will create a single view application. For this app first we add the iAD framework to implement iAd for iPhone. Here I will provide you code but it does not work in the iOS 6 Platform because a few things of iAd are deprecated from iOS 6, mostly due to the new screen sizes. If you want to check it's functioning you will run this app in the iOS 5 environment.

To better understand follow these instructions.

Step 1

Open Xcode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now Select Single View Application and click on "Next".

Step 4

Now provide your Product Name and Company Identifier.

Step 5

Select the location where you want to save your project and click on "Create".

Step 6

First we add the framework iAD framework that is required.

To import this framework we use the following.

Step 7

Click on the project and select "Build Phase".

Step 8

Click on the "+" icon to add a framework.

Step 9

Now click on the "Add" button.

framework-in-iPhone.png

Step 10

Now we write the following code.

AppDelegate.h

//

// iAdAppDelegate.h

// iAdapp

//

// Created by Sachin Bhardwaj on 17/01/13.

// Copyright (c) 2013 Sachin Bhardwaj. All rights reserved.

//

#import <UIKit/UIKit.h>

@class iAdViewController;

@interface iAdAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) iAdViewController *viewController;

@end

AppDelegate.m

//

// iAdAppDelegate.m

// iAdapp

//

// Created by Sachin Bhardwaj on 17/01/13.

// Copyright (c) 2013 Sachin Bhardwaj. All rights reserved.

//

#import "iAdAppDelegate.h"

#import "iAdViewController.h"

@implementation iAdAppDelegate

- (void)dealloc

{

[_window release];

[_viewController release];

[super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Override point for customization after application launch.

self.viewController = [[[iAdViewController alloc] initWithNibName:@"iAdViewController" bundle:nil] autorelease];

self.window.rootViewController = self.viewController;

[self.window makeKeyAndVisible];

return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application

{

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application

{

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application

{

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application

{

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application

{

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end


ViewController.h

//

// iAdViewController.h

// iAdapp

//

// Created by Sachin Bhardwaj on 17/01/13.

// Copyright (c) 2013 Sachin Bhardwaj. All rights reserved.

//

#import <UIKit/UIKit.h>

#import <iAd/iAd.h>

@interface iAdViewController : UIViewController

<ADBannerViewDelegate>

@property (strong, nonatomic) ADBannerView *bannerView;

@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end


ViewController.m

//

// iAdViewController.m

// iAdapp

//

// Created by Sachin Bhardwaj on 17/01/13.

// Copyright (c) 2013 Sachin Bhardwaj. All rights reserved.

//

#import "iAdViewController.h"

@interface iAdViewController ()

@end

@implementation iAdViewController

@synthesize tableView, bannerView;

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.

}

#pragma mark - View lifecycle

- (void)viewDidLoad {

bannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];

bannerView.requiredContentSizeIdentifiers = [NSSet setWithObjects:

ADBannerContentSizeIdentifierPortrait,

ADBannerContentSizeIdentifierLandscape, nil];

bannerView.delegate = self;

[super viewDidLoad];

}

- (void)viewDidUnload

{

[super viewDidUnload];

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

self.tableView = nil;

}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner

{

tableView.tableHeaderView = bannerView;

}

- (void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

}


- (void)viewDidAppear:(BOOL)animated

{

[super viewDidAppear:animated];

}

- (void)viewWillDisappear:(BOOL)animated

{

[super viewWillDisappear:animated];

}

- (void)viewDidDisappear:(BOOL)animated

{

[super viewDidDisappear:animated];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:

(UIInterfaceOrientation)interfaceOrientation {

if (UIInterfaceOrientationIsLandscape(interfaceOrientation))

bannerView.currentContentSizeIdentifier =

ADBannerContentSizeIdentifierLandscape;

else

bannerView.currentContentSizeIdentifier =

ADBannerContentSizeIdentifierPortrait;

return YES;

}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave

{

return YES;

}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner

{

}

- (void)bannerViewWillLoadAd:(ADBannerView *)banner

{

}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error

{

}

-(void) didFailToReceiveAdWithError:(NSError *)error

{

}

@end

Step 11

Click on the "Run" button to show output.

Step 12

Output1 in iPhone:

Output1-in-iPhone.png

Output2 in iPhone:

Output2-in-iPhone..png

Output3 in iPhone:

Output3-in-iPhone..png

Output4 in iPhone:

Output4-in-iPhone..png

Output5 in iPhone:

Output5-in-iPhone..png

Output6 in iPhone:

Output6-in-iPhone..png

Output7 in iPhone:

Output7-in-iPhone..png

Output8 in iPhone:

Output8-in-iPhone..png

To learn more about iAd check it out at:


http://stackoverflow.com/questions/12523571/iad-not-working-on-ios-6

Up Next
    Ebook Download
    View all
    Learn
    View all