且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-01 14:14:22

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



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



  MyScene.m 

if([viewController showIAd])//试图显示一个n ad
{
NSLog(@showIAd返回YES,广告显示);
//什么都不做。等待viewDidAppear被调用。
}
其他
{
//没有广告准备就绪,做广告之后需要做的事情。
}

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

  viewController.m 

- (void) viewDidAppear:(BOOL)动画
{
NSLog(@viewDidAppear);

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

//做广告之后需要做的事情。
}
}

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

希望这会有所帮助:)


We show interstitial iAds between levels. Since iAds appear to require quite a bit of memory (we used to receive many memory warnings when they are shown and experienced some crashes), we do not load the new view before the interstitial has been closed (so, we first unload the game view, then we show the interstitial, then we load the game view).

For this, we used the old/deprecated way of showing interstitials:

  1. allocate ADInterstitialAd and set delegate

    _interstitial = [[ADInterstitialAd alloc] init];
    _interstitial.delegate = self;
    

  2. when ready, present Ad in some viewcontroller:

    [_interstitial presentFromViewController:_rootViewController];
    

  3. listen to delegate methods to detect when the user closed the interstitial ad:

    - (void)interstitialAdActionDidFinish:(ADInterstitialAd *)interstitialAd
    {
        [self proceedToNextLevel];
    }
    

This used to work in iOS 7. In iOS8 however, while most of the delegate functions get called, interstitialAdActionDidFinish does not get called (interstitialAdDidUnload does get called, but only like 5 minutes later).

So, there seems to be some new way of showing interstitial ads through a category on UIViewController: https://developer.apple.com/library/ios/documentation/iAd/Reference/UIViewController_iAd_Additions/index.html#//apple_ref/occ/instm/UIViewController/shouldPresentInterstitialAd

So the new way would be:

  1. prepare ads through static method call:

    [UIViewController prepareInterstitialAds];
    

  2. when ready, request display of ad:

    _rootViewController.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual;
    [_rootViewController requestInterstitialAdPresentation];
    

This does show the interstitial - however, since there is no delegate anymore, there is no way of telling when the user closed the interstitial.

So the question is: How on iOS 8 (and also compatible to iOS 7) can we tell when a user closed an interstitial Ad?

//edit: I also tried to query

    viewController.isPresentingFullScreenAd

repeatedly with a timer, but while this works on iOS 7, on iOS 8 the property is always returning true, even after the user closed the interstitial ad

When an interstitial iAd closes, viewDidAppear is called in your VC.

Here's how I implement...

I call the following code from my SKScene when I want to show an ad.

    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.
    }

When an iAd is displayed I set a BOOL called currentlyShowingAnIAd. When viewDidAppear runs it knows it's returning from an ad.

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

Hope this helps :)