iOS7:如何等待requestInterstitialAdPresentation完成 [英] iOS7: How to wait for requestInterstitialAdPresentation to finish

查看:78
本文介绍了iOS7:如何等待requestInterstitialAdPresentation完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的视图控制器上实现了一种方法(通过协议),我的游戏引擎调用了该方法来展示非页内广告.

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

我想防止播放全屏广告时控制环返回到调用者,并且不影响广告清理和延迟处理时发生的SKScene对象的后续动画.

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

我知道iAD模型在iOS7中更简单,不需要实现委托等.但是等待插页式广告在iOS7中完成的正确方法是什么. (请注意,我使用的是SpriteKit,而不是情节提要).我必须恢复使用iOS6或更少的样式委托吗?

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

推荐答案

上面的代码在iOS8上不起作用(已在模拟器的iPhone 4s上测试),它将无限循环.所以我不得不更改它.

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.

首先,在视图控制器中,创建一个辅助方法来暂停和取消暂停当前的SKView

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

关键是要确保在调用presentFullScreenAd方法之后,针对视图中的节点对代码执行块操作,以免您担心何时出现广告".走了,执行一下这段代码".

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.

这篇关于iOS7:如何等待requestInterstitialAdPresentation完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆