iOS Swift:iAd Interstitial在被解雇后留下黑屏

时间:2015-07-11 04:15:45

标签: ios objective-c xcode swift iad

我试图在我的游戏中添加广告,每次用户输掉时我都会通过视图控制器展示我的游戏。有时,当广告加载时,我在屏幕上方的游戏顶部显示全屏插页式广告。

我的问题是插页式广告没有关闭按钮,所以我添加了一个,这样用户就不会觉得每次出现广告时都不得不点按广告。当用户点击关闭按钮时,广告被解除,并且再次显示游戏视图控制器。问题是,偶尔(随机,可能是第一次或经过几次)后,广告被解雇并留下黑屏。

为什么会发生这种情况?我试着弄清楚了几天,一直在摸不着头脑。

谢谢!这是我的代码:

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

对于冗长的帖子感到抱歉,谢谢!!!!!

3 个答案:

答案 0 :(得分:1)

不完全确定为什么会发生这种情况,但解决此问题的一种方法是调用屏幕上显示游戏的功能。我无法通过您给出的上述代码确切地说明代码行,但它可能看起来像这样;

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

答案 1 :(得分:1)

出现同样的问题,关闭广告后会出现黑屏。

我最终使用UIViewController来包含广告,然后在广告结束后解除了视图控制器。这更清洁,让您免费进行呈现过渡。

以下是展示广告的代码:

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

以下是代码解雇的内容:

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

答案 2 :(得分:0)

我在一段时间后想出来了,我删除了所有插入插页式广告的语句,而我只是在退出视图控制器时才这样做了。之后屏幕停止变黑。不知道为什么会这样,但确实如此。如果有人希望发光,那将是受欢迎的!