adMob iOS简历(返回app按钮)

时间:2016-04-08 06:36:04

标签: ios timer admob resume

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

流程是:

  • 1 - 游戏开始
  • 2 - 展示广告
  • 3 - 点击横幅并暂停计时器
  • 4 - oper safari
  • 5 - 点击"返回我的应用程序"链接/按钮(iOS功能)
  • 6 - 返回app和restar计时器(问题

我已经实现了所有adMob事件方法(并插入了restar计时器代码),但我无法摆脱这个问题。 该代码有效,因为它适用于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;
}

1 个答案:

答案 0 :(得分:1)

在此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]];

然后在viewDidAppearviewWillAppear之后,编写此函数

    - (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来监听应用何时返回活动状态。当您的应用变为活动状态时,系统会自动调用UIApplicationDidBecomeActiveNotification(这是一个应用委托方法)。如果是,则自动调用方法(void)applicationDidBecomeActiveNotification并调用该方法中的方法。你有一个布尔标志来查看该应用程序是否从Safari返回,因为如果用户在广告被推送时切换到另一个应用程序,您的应用程序可以从任何其他应用程序返回。最后,您将VC作为观察者删除,以避免内存泄漏。