你想要的是一个GADBannerView
各种单身人士。您可能可以创建一个包装类来充当 adView 的单例,例如:
@interface GADMasterViewController : UIViewController {
GADBannerView *adBanner_;
BOOL didCloseWebsiteView_;
BOOL isLoaded_;
id currentDelegate_;
}
只需确保GADMasterViewController
总是返回一个单例:
+(GADMasterViewController *)singleton {
static dispatch_once_t pred;
static GADMasterViewController *shared;
// Will only be run once, the first time this is called
dispatch_once(&pred, ^{
shared = [[GADMasterViewController alloc] init];
});
return shared;
}
有一个方法可以重置当前持有 adView 的视图控制器:
-(void)resetAdView:(UIViewController *)rootViewController {
// Always keep track of currentDelegate for notification forwarding
currentDelegate_ = rootViewController;
// Ad already requested, simply add it into the view
if (isLoaded_) {
[rootViewController.view addSubview:adBanner_];
} else {
adBanner_.delegate = self;
adBanner_.rootViewController = rootViewController;
adBanner_.adUnitID = kSampleAdUnitID;
GADRequest *request = [GADRequest request];
[adBanner_ loadRequest:request];
[rootViewController.view addSubview:adBanner_];
isLoaded_ = YES;
}
}
然后展示您的广告只需执行以下操作:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
shared = [GADMasterViewController singleton];
[shared resetAdView:self];
}
您可能还需要设置委托来转发通知,因为 AdMob SDK 无法很好地处理在请求过程中对其进行更改的委托。
您可以找到有关此的博客文章here http://googleadsdeveloper.blogspot.com/2012/04/creating-gadbannerview-singleton-in.html.