iPhone – Create an iAd Application

Starting from iPhone OS4, you can add an advertisement banner to your iPhone Application using the iAd framework. I found a great article talking about adding iAd to the iPhone app. Here is an example of a empty view based application with iAd.

1. Open Xcode and create a new View-based Application (iAdProject)
2. Add iAd.framework to the project
3. Modify the iAdProjectViewController.h

//
//  iAdProjectViewController.h
//  iAdProject
//
//  Created by ykyuen on 21/07/2010.
//

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>

@interface iAdProjectViewController : UIViewController <ADBannerViewDelegate>{
	ADBannerView *adBannerView;
}

@property(nonatomic, retain) ADBannerView *adBannerView;

@end

 

4. Modify the iAdProjectViewController.m

//
//  iAdProjectViewController.m
//  iAdProject
//
//  Created by ykyuen on 21/07/2010.
//

#import "iAdProjectViewController.h"

@implementation iAdProjectViewController
@synthesize adBannerView;


- (void)viewDidLoad {
	[super viewDidLoad];
	adBannerView = [[ADBannerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 50.0)];
	adBannerView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
	adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
	[adBannerView setHidden:YES];
	adBannerView.delegate = self;
	[self.view addSubview:adBannerView];
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
	[banner setHidden:NO];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
	[banner setHidden:YES];
}

- (void)dealloc {
	[adBannerView release];
	[super dealloc];
}

@end

 

5. Try it in the simulator

 

There are much more information about iAd in the reference below.
Done =)

Reference: How to Add iAd Banner in iPhone App

9 thoughts on “iPhone – Create an iAd Application”

  1. Thanks for the link.

    In your post: iPhone – Configuring the AVAudioSession and Output Audio to iPhone Speaker, you commented on using

    1. create a switching function in the appDelegate which take a bool as input parameter and change the audio route.
    2. then in your view, add a button and implement the click action which called the appDelegate to change the route.
    i am not sure if it works or not since i dun know if the route can be changed on the fly.
    can you explain this in a little more detail as i don’t know if i’ve done it right.
    Thanks

    Like

  2. I have read the post configuring the AVAudioSession and Output Audio 2 iPhone Speaker, and i think, just like someone Somewhere, that comment needs a little more detail. I am really stuck on doing this 2.

    Like

    1. hi bob, please post the comment in the relevant post.
      by the way, could you post your code and let me see what’s the problem is?

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.