iOS7: How to wait for requestInterstitialAdPresentation to finish

ぃ、小莉子 提交于 2019-12-11 07:42:37

问题


I have implemented a method on my view controller (via a protocol) which my game engine calls to present Interstitial advertisements.

I want to prevent the control loop from returning to the caller while a full screen advertisement is in play, and not affect subsequent animations of SKScene objects happening while the ad cleans up and delays processing.

I have achieved this with the following code, which checks if an advert will be presented, and blocks the current thread until the advert is no longer being shown, and the view isn't unloading for that ad. (Without the dismiss check, my transitions appeared to be effected too early).

I know that the iAD model is simpler in iOS7, no need to implement delegates etc. But what is the right way of waiting for interstitial ads to complete in iOS7. (Note, I'm using SpriteKit, not storyboarding). Do I have to revert to using iOS6 or less style delegates?

-(void)presentFullScreenAd
{
    if( [self requestInterstitialAdPresentation] )
    {
        while( self.isPresentingFullScreenAd  || self.isBeingDismissed )
        {
            // wait till we have finished before continuing
            [NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
        }
    }
}

回答1:


The code above does not work on iOS8 (tested on iPhone 4s in simulator), it will loop infinitely. So I've had to change it.

This isn't perfect, there is still a bit of jerkiness between transitions but is less hacky.

First, in the view controller, create a helper method to pause and un-pause the current SKView

-(void)pauseCurrentSKView:(bool)paused
{
    if( [self.view isKindOfClass:[SKView class] ] )
    {
        SKView* pView = (SKView*)self.view;
        pView.paused = paused;
    }
}

then make your full screen ad method pause the view immediately but un-pause if adverts didn't show. You'll un-pause later.

-(void)presentFullScreenAd
{
    // pause ASAP - requestInterstitialAdPresentation may take some time
    // so you need to pause before your transitions kick in.
    [self pauseCurrentSKView:YES];

    if( [self requestInterstitialAdPresentation]==NO )
    {
        [self pauseCurrentSKView:NO];
    }
}

Then handle when your view comes back into play - and resume animations.

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if( self.isPresentingFullScreenAd )
    {
        // we are about to stop presenting full screen ads
        [self pauseCurrentSKView:NO];
    }
    else
    {
        // we are presenting the normal view or the full screen ad
    }
}

The key thing is to make sure after you call the presentFullScreenAd method, that you put your code in follow on block actions against nodes in the view, so that you don't have to worry about 'when the ads are gone, do this bit of code'.

If anyone has any refinements - please ad. I've purposefully not included extra code where I only launch fullscreen adverts every three minutes - easy to do.



来源:https://stackoverflow.com/questions/25825535/ios7-how-to-wait-for-requestinterstitialadpresentation-to-finish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!