在IBAction按钮上加载插页式广告

时间:2017-05-09 18:08:06

标签: ios swift admob interstitial

点击我的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()

}

}

3 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

我认为您的问题与插页式广告上present方法的同步性有关。我不知道实现是什么,但似乎广告的演示文稿不会阻止调用它的线程。您应尝试实施GADInterstitialDelegate以在广告出现时抓住该事件,然后从那里开始接听电话。

答案 2 :(得分:0)

试试这个:

  

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