adMob iOS 恢复(返回应用按钮)

adMob iOS resume (back to app button)

我有一个带计时器的小游戏。 我正在实施 adMob 以获利,但我无法在用户点击横幅并返回应用程序后重新启动 timer/ads。

流程是:

我已经实现了所有 adMob 事件方法(并插入重启计时器代码),但我无法解决这个问题。 该代码之所以有效,是因为它适用于 iAds(我正在迁移到 adMob)。

感谢任何帮助。 谢谢

编辑: 这是代码:

    /// Tells the delegate an ad request loaded an ad.
- (void)adViewDidReceiveAd:(GADBannerView *)adView {
  NSLog(@"adViewDidReceiveAd");
  self.pauseTimer = NO;
}

/// Tells the delegate an ad request failed.
- (void)adView:(GADBannerView *)adView
    didFailToReceiveAdWithError:(GADRequestError *)error {
  NSLog(@"adView:didFailToReceiveAdWithError: %@", [error localizedDescription]);
  self.pauseTimer = NO;
}

/// Tells the delegate that a full screen view will be presented in response
/// to the user clicking on an ad.
- (void)adViewWillPresentScreen:(GADBannerView *)adView {
  NSLog(@"adViewWillPresentScreen");
  self.pauseTimer = NO;
}

/// Tells the delegate that the full screen view will be dismissed.
- (void)adViewWillDismissScreen:(GADBannerView *)adView {
  NSLog(@"adViewWillDismissScreen");
  self.pauseTimer = NO;
}

/// Tells the delegate that the full screen view has been dismissed.
- (void)adViewDidDismissScreen:(GADBannerView *)adView {
  NSLog(@"adViewDidDismissScreen");
  self.pauseTimer = NO;
}

/// Tells the delegate that a user click will open another app (such as
/// the App Store), backgrounding the current app.
- (void)adViewWillLeaveApplication:(GADBannerView *)adView {
  NSLog(@"adViewWillLeaveApplication");
  self.pauseTimer = YES;
}

在这个VC中创建一个属性来存储这个

@property (nonatomic) BOOL didGoToSafari;

- (void)adViewWillLeaveApplication:(GADBannerView *)adView {
  NSLog(@"adViewWillLeaveApplication");
  self.pauseTimer = YES;
  self.didGoToSafari = YES;
}

在广告将在 viewWillAppearviewDidAppear 中显示之前显示的 VC 中,您应该放置此代码

  [[NSNotificationCenter defaultCenter]
   addObserver:self
   selector:@selector(applicationDidBecomeActiveNotification:)
   name:UIApplicationDidBecomeActiveNotification
   object:[UIApplication sharedApplication]];

然后在viewDidAppear或者viewWillAppear之后,写这个函数

    - (void)applicationDidBecomeActiveNotification:(NSNotification *)notification {

if (self.didGoToSafari = YES){

      self.pauseTimer = NO;
      self.didGoToSafari = NO;
}
    }

viewWillDisappear

[[NSNotificationCenter defaultCenter] removeObserver:self                                  name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];

基本上您正在做的是监听应用程序是否再次激活。如果是,请检查它是否从 Safari 返回。它并不完美,因为您可能正在使用该应用程序,用户转到 Safari,然后不返回或关闭游戏。然后他们可以稍后使用 Safari,然后返回游戏,游戏将再次开始 运行。在 AppDelegate 中可能有一些控制流可以用来围绕这个进行编码,但通常这段代码应该做到这一点。

编辑:根据您对理解它的评论,这里是完整的解释。

您正在使用 NSNotification 来侦听应用 return 何时进入活动状态。 UIApplicationDidBecomeActiveNotification 在您的应用激活时自动调用(这是一个应用委托方法)。当它这样做时,方法 (void)applicationDidBecomeActiveNotification 被自动调用,并且该方法中的方法被调用。你有一个布尔标志来查看应用程序是否从 Safari returning,因为如果用户在推送广告时切换到另一个应用程序,你的应用程序可以从任何其他应用程序 return。最后,您将 VC 作为观察者移除以避免内存泄漏。