在我所有的视图控制器AdMob的?有的、视图、控制器、在我所

2023-09-09 22:07:18 作者:孤身成王ヾ

我已经实现的AdMob和放大器;一切似乎工作, 但我wonderd,我怎么能放的旗帜在我所有的视图控制器? 现在,我只有在RootViewController的旗帜。 我共有4视图控制器。 谢谢你。

I have implemented AdMob & everything seems to work, But i wonderd, how can i put the banner in all of my view controllers? For now, i have the banner only on the RootViewController. I have total of 4 view controllers. Thanks.

推荐答案

你想要的这里是各种各样的 GADBannerView 单。你也许可以创建一个包装类作为一个单身的你AD浏览报,所以是这样的:

What you want here is a GADBannerView singleton of sorts. You can probably create a wrapping class to act as a singleton for your adView, so something like:

@interface GADMasterViewController : UIViewController {
  GADBannerView *adBanner_;
  BOOL didCloseWebsiteView_;
  BOOL isLoaded_;
  id currentDelegate_;
}

和公正,确保 GADMasterViewController 总是返回一个单:

And just make sure that GADMasterViewController always returns a singleton:

+(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;
}

有其重置当前视图控制器的控股到AD浏览报的方法:

Have a method which resets the current view controller that's holding on to the 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;
  }
}

然后展示广告是只是一个问题:

Then displaying your ad is just a matter of:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    shared = [GADMasterViewController singleton];
    [shared resetAdView:self];
}

您可能需要设置代理转发的通知,以及因为AdMob的SDK不采取行动很好地代表了请求的中间变化就可以了。

You probably need to set up a delegate to forward notifications as well since the AdMob SDK doesn't act well to delegates changing on it in the middle of a request.

您可以找到有关这一篇博客文章here.

You can find a blog post about this here.