小智 5
你想要的是一个GADBannerView单一的单身.您可以创建一个包装类来充当adView的单例,如下所示:
@interface GADMasterViewController : UIViewController {
GADBannerView *adBanner_;
BOOL didCloseWebsiteView_;
BOOL isLoaded_;
id currentDelegate_;
}
Run Code Online (Sandbox Code Playgroud)
并确保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;
}
Run Code Online (Sandbox Code Playgroud)
有一个方法可以重置当前持有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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后展示您的广告只需要:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
shared = [GADMasterViewController singleton];
[shared resetAdView:self];
}
Run Code Online (Sandbox Code Playgroud)
您可能还需要设置一个委托以转发通知,因为AdMob SDK不能很好地代表在请求过程中更改它的代理.
你可以在这里找到关于这个的博客文章.
我不知道 adMob 是如何工作的,但像其他所有东西一样,您可以创建一个,BaseViewController在其中添加您的 adMob (在方法中viewDidLoad),然后所有其他 viewController 都可以子类化 this BaseViewController。只需调用[super viewDidLoad];你viewDidLoad的 viewController 的方法,你就会得到它......
希望这能解决您的问题...:)
| 归档时间: |
|
| 查看次数: |
2473 次 |
| 最近记录: |