简体   繁体   English

iOS Swift:解雇后,iAd插页式广告仍显示黑屏

[英]iOS Swift: iAd Interstitial leaves black screen after dismissed

I'm trying to add ads into my game, every time the user loses I present my game over view controller. 每当用户迷路时,我都会尝试在广告中添加广告,而我会通过视图控制器展示游戏。 On occasions, when an ad loads I have a full screen interstitial is shown on top of the game over screen. 有时,当广告加载时,我会在屏幕上方的游戏顶部显示全屏插页式广告。

My problem is the interstitial doesn't come with a close button, so I added one so the user doesn't feel forced to tap on the ad every time it comes up. 我的问题是插页式广告没有关闭按钮,因此我添加了一个按钮,这样用户就不会每次看到广告时都被迫点击。 When the user clicks the close button, the ad is dismissed and the game over view controller is once again shown. 当用户单击关闭按钮时,该广告将被撤消,并且再次显示游戏结束视图控制器。 Problem is, once in a while (at random, maybe the first time or after a couple of runs through) the ad is dismissed and leaves a black screen. 问题是,有时(随机出现,可能是第一次或几次运行之后),广告被撤消并留下黑屏。

Any ideas why this is happening? 任何想法为什么会这样? I've been scratching my head for a few days trying to figure this out. 为了解决这个问题,我几天来一直在挠头。

Thanks!! 谢谢!! Here is my code: 这是我的代码:

// Ad variables
var interstitialAdView: UIView = UIView()
var interstitial:ADInterstitialAd!
var intersitialTracker = false
var closeButton:UIButton!

override func viewDidAppear(animated: Bool) {
    if !intersitialTracker {
            loadInterstitialAd()
    }
}

// Buttons to restart game, and return to home screen
@IBAction func restartButtonPressed(sender: AnyObject) {

    intersitialTracker = false
    interstitial = nil
    delegate?.gameOverViewControllerDidPressRestart(self)

}

@IBAction func homeButtonPressed(sender: AnyObject) {
    interstitial = nil
    delegate?.gameOverViewControllerDidPressHomebutton(self)


}

func loadInterstitialAd() {
    if interstitial != nil {
        interstitial.delegate = nil
    }
    interstitial = ADInterstitialAd()
    interstitial.delegate = self
    intersitialTracker = true
}

func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) {
}

func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
    interstitialAdView = UIView()
    interstitialAdView.frame = self.view.bounds
    view.addSubview(interstitialAdView)

    closeButton = UIButton(frame: CGRect(x: 20, y:  20, width: 20, height:20))
    closeButton.setBackgroundImage(UIImage(named: "close"), forState: UIControlState.Normal)


    closeButton.addTarget(self, action: Selector("close"), forControlEvents: UIControlEvents.TouchDown)
    self.view.addSubview(closeButton)
    interstitialAd.presentInView(interstitialAdView)
}

// Called when user leaves the ad
func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
    interstitialAdView.removeFromSuperview()
    closeButton.removeFromSuperview()
    interstitial = nil
    closeButton = nil
}

// Called when user goes in ad
func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {
    return true

}

func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
}

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
    interstitialAdView.removeFromSuperview()
    closeButton.removeFromSuperview()
    interstitial = nil
    closeButton = nil
}

// Close button for ads
func close() {
    if closeButton != nil {
        interstitialAdView.removeFromSuperview()
        closeButton.removeFromSuperview()
        interstitial = nil
        closeButton = nil
    }
}

Sorry for the lengthy post, thanks!!!!! 抱歉,冗长的帖子,谢谢!!!!

Not entirely sure why this is happening to you but one way to work around it is to call the function that shows the game over screen. 不能完全确定为什么会发生这种情况,但是解决该问题的一种方法是调用在屏幕上显示游戏的函数。 I can't tell exactly which line of code does by the above code you have given but it could look something like this; 我不能通过上面给出的代码确切地知道哪一行代码可以执行,但是看起来可能像这样;

func close() {
if closeButton != nil {
    interstitialAdView.removeFromSuperview()
    closeButton.removeFromSuperview()
    interstitial = nil
    closeButton = nil
    self.GameOverScreen()
}

Had the same problem, where it would show a black screen after closing the ad. 遇到相同的问题,在关闭广告后会显示黑屏。

I ended up using a UIViewController to contain the ad, and then dismissed the view controller upon the ad finishing. 我最终使用UIViewController包含广告,然后在广告完成后关闭了视图控制器。 This is much cleaner and gets you the presenting transition for free. 这更加干净,可以让您免费呈现演示文稿。

Here's what the code looks like to present the ad: 这是展示广告的代码:

- (void)interstitialAdDidLoad:(ADInterstitialAd *)interstitialAd
{
    UIViewController *controller = [[UIViewController alloc] init];
    [self.iadInterstitial presentInView:controller.view];
    // Nearly everyone has this logic in their UIViewController. So you may
    // have to replace 'self.viewController' with just 'self'.
    [self.viewController presentViewController:controller animated:YES completion:nil];
}

Here's what the code looks like to dismiss: 代码看起来像这样:

- (void)interstitialAdActionDidFinish:(ADInterstitialAd *)interstitialAd
{
    [self.viewController dismissViewControllerAnimated:YES completion:^{
        self.iadInterstitial.delegate = nil;
        self.iadInterstitial = nil;
    }];
}

I figured it out after some time, I removed all statements where interstitial was assigned nil, instead I just did it when I exited the view controller. 一段时间后,我发现了这一点,我删除了所有非页内广告分配为nil的语句,而是在退出视图控制器时才执行此操作。 The screen stopped getting black after that. 之后,屏幕停止变黑。 No sure why this worked, but it did. 不知道为什么这样做有效,但确实可行。 If someone wants to shine some light it will be welcomed! 如果有人想发光一点,将受到欢迎!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM