【问题标题】:Interstitial iAd has no close 'X' button插页式 iAd 没有关闭“X”按钮
【发布时间】:2014-09-17 16:57:29
【问题描述】:

我正在使用 Swift 在我的应用中实现插页式广告。我设法让它显示插页式广告,但没有关闭“X”按钮来摆脱广告。这是为什么呢?

【问题讨论】:

    标签: ios xcode swift iad interstitial


    【解决方案1】:

    我也找不到为什么内置框架不提供关闭按钮的答案,但我最终使用了以下解决方案。

    //iAD interstitial definition with custom placeHolderView and close button
    //you do not need to add anything to interface builder. All is done with code
    
    var interstitial:ADInterstitialAd!
    var placeHolderView:UIView!
    var closeButton:UIButton!
    
    //Full Implemantation of iAD interstitial and delegate methods
    
     func cycleInterstitial(){
     // create a new interstitial. We set the delegate so that we can be notified 
        interstitial = ADInterstitialAd()
        interstitial.delegate = self;
    }
    
    func presentInterlude(){
        // If the interstitial managed to load, then we'll present it now.
        if (interstitial.loaded) {
    
            placeHolderView = UIView(frame: self.view.frame)
            self.view.addSubview(placeHolderView)
    
            closeButton = UIButton(frame: CGRect(x: 270, y:  25, width: 25, height: 25))
           //add a cross shaped graphics into your project to use as close button
            closeButton.setBackgroundImage(UIImage(named: "cross"), forState: UIControlState.Normal)
            closeButton.addTarget(self, action: Selector("close"), forControlEvents: UIControlEvents.TouchDown)
            self.view.addSubview(closeButton)
    
    
            interstitial.presentInView(placeHolderView)
        }
    }
    
    // iAd Delegate Mehtods
    
    // When this method is invoked, the application should remove the view from the screen and tear it down.
    // The content will be unloaded shortly after this method is called and no new content will be loaded in that view.
    // This may occur either when the user dismisses the interstitial view via the dismiss button or
    // if the content in the view has expired.
    
    func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!){
        placeHolderView.removeFromSuperview()
        closeButton.removeFromSuperview()
        interstitial = nil
    
        cycleInterstitial()
    }
    
    
    func interstitialAdActionDidFinish(_interstitialAd: ADInterstitialAd!){
        placeHolderView.removeFromSuperview()
        closeButton.removeFromSuperview()
        interstitial = nil
    
        println("called just before dismissing - action finished")
    
    }
    
    // This method will be invoked when an error has occurred attempting to get advertisement content.
    // The ADError enum lists the possible error codes.
    func interstitialAd(interstitialAd: ADInterstitialAd!,
        didFailWithError error: NSError!){
            cycleInterstitial()
    }
    
    
    //Load iAd interstitial
    func dislayiAdInterstitial() {
        //iAd interstitial
        presentInterlude()
    }
    
    
    func close() {
        placeHolderView.removeFromSuperview()
        closeButton.removeFromSuperview()
        interstitial = nil
    
    }
    

    【讨论】:

      【解决方案2】:

      使用 interstitial=InterstitialAd() 会导致我和你一样的问题。

      但是,如果您在 AppDelegate 的应用程序函数中包含以下代码:

      UIViewController.prepareInterstitialAds()
      

      然后,您可以使用以下方法生成插页式广告:

      viewController.requestInterstitialAdPresentation()
      

      不过,我不确定如何获取回调。

      【讨论】:

        【解决方案3】:

        iOS 9.2.1,Xcode 7.2.1,启用 ARC

        请阅读我的帖子:

        https://stackoverflow.com/a/35467510/4018041

        我仍然使用presentFromViewController: 方法。

        希望这会有所帮助!干杯。

        【讨论】:

          猜你喜欢
          • 2023-04-09
          • 1970-01-01
          • 1970-01-01
          • 2021-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多