[iphone-adMob]程序添加Admob样例
#import "ProgrammaticAdViewController.h"
#import "AdMobView.h"
@implementation ProgrammaticAdViewController
// The designated initializer. Override if you create the controller programmatically
// and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.title = @"Programmatic Ad";
}
return self;
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
// get the window frame here.
CGRect appFrame = [UIScreen mainScreen].applicationFrame;
UIView *view = [[UIView alloc] initWithFrame:appFrame];
// making flexible because this will end up in a navigation controller.
view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view = view;
[view release];
// Request an ad
adMobAd = [AdMobView requestAdWithDelegate:self]; // start a new ad request
[adMobAd retain]; // this will be released when it loads (or fails to load)
}
// Request a new ad. If a new ad is successfully loaded, it will be animated into location.
- (void)refreshAd:(NSTimer *)timer {
[adMobAd requestFreshAd];
}
- (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 {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[adMobAd release];
[refreshTimer invalidate];
[super dealloc];
}
#pragma mark -
#pragma mark AdMobDelegate methods
- (NSString *)publisherId {
return @"a14bf4c2e7337ed"; // this should be prefilled; if not, get it from www.admob.com
}
- (UIViewController *)currentViewController {
return self;
}
- (UIColor *)adBackgroundColor {
return [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
- (UIColor *)primaryTextColor {
return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
- (UIColor *)secondaryTextColor {
return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}
// Sent when an ad request loaded an ad; this is a good opportunity to attach
// the ad view to the hierachy.
- (void)didReceiveAd:(AdMobView *)adView {
NSLog(@"AdMob: Did receive ad");
// get the view frame
CGRect frame = self.view.frame;
// put the ad at the bottom of the screen
adMobAd.frame = CGRectMake(0, frame.size.height - 48, frame.size.width, 48);
[self.view addSubview:adMobAd];
[refreshTimer invalidate];
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:AD_REFRESH_PERIOD target:self selector:@selector(refreshAd:) userInfo:nil repeats:YES];
}
// Sent when an ad request failed to load an ad
- (void)didFailToReceiveAd:(AdMobView *)adView {
NSLog(@"AdMob: Did fail to receive ad");
[adMobAd removeFromSuperview]; // Not necessary since never added to a view, but doesn't hurt and is good practice
[adMobAd release];
adMobAd = nil;
// we could start a new ad request here, but in the interests of the user's battery life, let's not
}
作者:Alexliu(alex dotNet Learning)
出处:http://alexliu.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,转载请注明。并且保留文章链接。否则保留追究法律责任的权利。