// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// For Testing
GADRequest *rq = [GADRequest request];
rq.testing = YES;
// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest:rq];
顯示的廣告如下圖。
不過,該設定隻對模拟器有效,在實際裝置上運作時,仍然顯示真實的廣告。如下圖。
例程
接下來,通過一個例程說明一下AdMob廣告的添加過程。
1. Xcode 4中建立一個「Tab Bar Application」新程式「AdMobTabBar」
// Create a view of the standard size at the bottom of the screen.
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
self.view.frame.size.height -
TABBAR_HEIGHT,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
// delegate の設定
bannerView_.delegate = self;
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// For Testing
GADRequest *rq = [GADRequest request];
rq.testing = YES;
// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest:rq];
#import "FirstViewController.h"
#define MY_BANNER_UNIT_ID @"xxxxxxxxxxxxxxx"
#define ANIMATION_DURATION 0.5f
#define TABBAR_HEIGHT 49.0f
@implementation FirstViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
// Create a view of the standard size at the bottom of the screen.
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
self.view.frame.size.height -
TABBAR_HEIGHT,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
// delegate の設定
bannerView_.delegate = self;
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// For Testing
GADRequest *rq = [GADRequest request];
rq.testing = YES;
// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest:rq];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc
{
bannerView_.delegate = nil;
[bannerView_ release];
[super dealloc];
}
- (void)adViewDidReceiveAd:(GADBannerView *)view {
[UIView animateWithDuration:ANIMATION_DURATION
animations:^{
bannerView_.center = CGPointMake(bannerView_.center.x, bannerView_.center.y-TABBAR_HEIGHT);
}];
}
@end