插页式iAD:如何在iOS 8上检测关闭?

时间:2014-12-01 15:48:08

标签: objective-c ios8 iad interstitial

我们在级别之间显示插页式广告。由于iAds似乎需要相当多的内存(我们曾经在显示并遇到一些崩溃时收到许多内存警告),因此我们不会在插页式广告关闭之前加载新视图(因此,我们首先卸载游戏视图,然后我们显示插页式广告,然后我们加载游戏视图)。

为此,我们使用旧的/弃用的方式显示插页式广告:

  1. 分配ADInterstitialAd并设置委托

    _interstitial = [[ADInterstitialAd alloc] init];
    _interstitial.delegate = self;
    
  2. 准备好后,在某个viewcontroller中显示广告:

    [_interstitial presentFromViewController:_rootViewController];
    
  3. 听取委托方法以检测用户何时关闭插页式广告:

    - (void)interstitialAdActionDidFinish:(ADInterstitialAd *)interstitialAd
    {
        [self proceedToNextLevel];
    }
    
  4. 这曾经在iOS 7中工作。但是,在iOS8中,虽然调用了大多数委托函数,但是不调用interstitialAdActionDidFinish(调用interstitialAdDidUnload,但仅在5分钟后调用)。

    因此,似乎有一种新的方式通过UIViewController上的类别显示插页式广告:https://developer.apple.com/library/ios/documentation/iAd/Reference/UIViewController_iAd_Additions/index.html#//apple_ref/occ/instm/UIViewController/shouldPresentInterstitialAd

    所以新的方式是:

    1. 通过静态方法调用准备广告:

      [UIViewController prepareInterstitialAds];
      
    2. 准备好后,请求显示广告:

      _rootViewController.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual;
      [_rootViewController requestInterstitialAdPresentation];
      
    3. 这确实显示了插页式广告 - 但是,由于没有委托,因此无法确定用户何时关闭了插页式广告。

      所以问题是:在用户关闭插页式广告时,我们如何判断iOS 8(以及与iOS 7的兼容性)?

      //编辑: 我也试过查询

          viewController.isPresentingFullScreenAd
      

      反复使用计时器,但虽然这适用于iOS 7,但在iOS 8上,即使用户关闭了插页式广告,该属性也始终返回true

1 个答案:

答案 0 :(得分:4)

当插页式广告联盟关闭时,您的VC会调用viewDidAppear。

以下是我的实施方式......

当我想要展示广告时,我会从我的SKScene中调用以下代码。

    MyScene.m

    if ([viewController showIAd]) // attempting to show an ad
    {
        NSLog(@"showIAd returned YES, ad shown");
        // Do nothing.  Wait for viewDidAppear to be called.
    }
    else
    {
        // No ad was ready, do what needs to happen after ad.
    }

当显示iAd时,我设置了一个名为currentShowingAnIAd的BOOL。当viewDidAppear运行时,它知道它从广告返回。

viewController.m

-(void)viewDidAppear:(BOOL)animated
{
    NSLog(@"viewDidAppear");

    if (self.currentlyShowingAnIAd)
    {
        self.currentlyShowingAnIAd = NO;

        // Do what needs to happen after ad.
    }
}

-(BOOL)showIAd
{
    NSLog(@"showIAd method run");
    self.currentlyShowingAnIAd = [self requestInterstitialAdPresentation];
    return self.currentlyShowingAnIAd;
}

希望这会有所帮助:)