【问题标题】:load interstitial ad on IBAction button在 IBAction 按钮上加载插页式广告
【发布时间】:2017-05-09 18:08:05
【问题描述】:

当我的 callButton 被点击时,它会响起一个电话号码,我想在拨打电话之前展示我的插页式广告。我已经在“IBAction 函数广告”按钮上测试了我的广告,它在按钮上有效,但是当我在 callButton func 上调用它时,它不起作用并直接拨打电话。

class ProfilePageViewController: UIViewController, MFMessageComposeViewControllerDelegate, UITableViewDelegate, UIAlertViewDelegate, GADInterstitialDelegate {

@IBAction func ad(_ sender: Any) {
    if self.interstitialAd.isReady {
        self.interstitialAd.present(fromRootViewController: self)
    }
}

@IBAction func callButton(_ sender: Any) {

    if let contactopt = contact{

        if let url = NSURL(string: "tel://\(contactopt)") {
            //    UIApplication.shared.openURL(url as URL)
            UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
        }
    }
}

var interstitialAd: GADInterstitial!

func reloadInterstitialAd() -> GADInterstitial {
    var interstitial = GADInterstitial(adUnitID: "ca-app-pub-/6966780536")
    interstitial.delegate = self
    interstitial.load(GADRequest())
    return interstitial
}

func interstitialDidDismissScreen(ad: GADInterstitial!) {
    self.interstitialAd = reloadInterstitialAd()
}

func interstitialDidDismissScreen(_ ad: GADInterstitial) {
    interstitialAd = reloadInterstitialAd()
}
override func viewDidLoad() {
    self.interstitialAd = GADInterstitial(adUnitID: "ca-app-pub-/6966780536")
    let request = GADRequest()
    request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
    self.interstitialAd.load(request)
    self.interstitialAd = reloadInterstitialAd()

    super.viewDidLoad()

}

}

【问题讨论】:

    标签: ios swift admob interstitial


    【解决方案1】:

    当然是打电话。这就是你告诉它与UIApplication.shared.open 做的事情。

    interstitialDidDismissScreen 中关闭广告后拨打电话。 还要检查是否有广告要展示,interstitialAd.isReady。如果没有可展示的广告,请直接拨打电话。

    你在viewDidLoad 中也做了两次同样的事情:

    // Sets up an ad
    self.interstitialAd = GADInterstitial(adUnitID: "ca-app-pub-/6966780536")
    let request = GADRequest()
    request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
    self.interstitialAd.load(request)
    // Creates a new ad
    self.interstitialAd = reloadInterstitialAd()
    

    只需致电self.interstitialAd = reloadInterstitialAd()

    【讨论】:

    • 您是否建议我离开“IBAction 函数广告”以显示整页广告,并在 interstitialDidDismissScreen 函数中调用我的 callButton 函数?
    • @hghg 对不起,我不明白你在问什么。
    【解决方案2】:

    我相信您的问题与插页式广告上的 present 方法的同步有关。我不知道实现是什么,但似乎广告的演示不会阻塞调用它的线程。您应该尝试实现 GADInterstitialDelegate 以在展示广告时获取事件,然后继续从那里拨打电话。

    【讨论】:

      【解决方案3】:

      试试这个:

      GADInterstitial 是一次性使用对象。这意味着一旦 显示插页式广告,hasBeenUsed 返回 true,插页式广告 不能用于加载其他广告。要请求其他插页式广告, 您需要创建一个新的 GADInterstitial 对象。最佳实践, 如上图,就是要有一个辅助方法来处理创建和 加载插页式广告。

      func createAndLoadInterstitial() -> GADInterstitial {
        var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        interstitial.delegate = self
        interstitial.load(GADRequest())
        return interstitial
      }
      
      func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        interstitial = createAndLoadInterstitial()
      }
      
      @IBAction func ad(_ sender: Any) {
          if self.interstitialAd.isReady {
              self.interstitialAd.present(fromRootViewController: self)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多