为什么admob插页式广告没有出现在iOS上?

时间:2017-05-09 22:10:26

标签: ios swift admob ibaction interstitial

单击adText按钮时,我的admob插页式广告未显示。我希望插页式广告能够展示,并在解散时运行我的text()功能。我希望在按下我的adCall按钮时发生同样的事情,但在插页式广告被解雇后才能运行我的call()函数。

enum RequestType{
    case call
    case text
}


var requestType: RequestType?

@IBAction func adText(_ sender: Any) {
    requestType = .text
    presentInterstitialAd()
    text()
}

@IBAction func adCall(_ sender: Any) {
    requestType = .call
    presentInterstitialAd()
    call()
}


func interstitialDidDismissScreen(_ ad: GADInterstitial) {
    interstitialAd = reloadInterstitialAd()
}

func presentInterstitialAd() {
    if self.interstitialAd.isReady {
        self.interstitialAd.present(fromRootViewController: self)
    }
}

func invokeRequest() {
    guard let requestType = requestType else {
        fatalError("NO request type specified!")    
    }
    switch requestType {
    case .call: invokeCall()
    case .text: invokeText()
    }
}

func invokeCall() {}
func invokeText() {}

func call() {   
    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)

        }
    }
}

func text(){
    if canSendText() {
        if let contactopt = contact{
            let messageVC = MFMessageComposeViewController()
            messageVC.recipients = [String(contactopt)]
            messageVC.messageComposeDelegate = self;

            self.present(messageVC, animated: false, completion: nil)}}
    else {

        let alertController = UIAlertController(title: "Cannot Send Text Message", message: "Your device is not able to send text messages.", preferredStyle: UIAlertControllerStyle.alert)
        let DestructiveAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive) {
            (result : UIAlertAction) -> Void in
            print("error")
        }

        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            (result : UIAlertAction) -> Void in
            print("OK")
        }

        alertController.addAction(DestructiveAction)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
}

1 个答案:

答案 0 :(得分:1)

据我所知,您只想在广告被呈现和解散后调用按钮的实际行为。在这种情况下,只需保存请求类型,展示广告,关闭广告,重新加载插页式广告并调用已保存的请求类型的请求。

首先,定义类型:

enum RequestType {
  case call
  case text
}

其次,更新视图控制器:

var requestType: RequestType?

@IBAction func adText(_ sender: Any) {
  requestType = .text
  presentInterstitialAd()
}

@IBAction func adCall(_ sender: Any) {
  requestType = .call
  presentInterstitialAd() 
}

func presentInterstitialAd() {
  if self.interstitialAd.isReady {
    self.interstitialAd.present(fromRootViewController: self)
  }
}

func interstitialDidDismissScreen(_ ad: GADInterstitial) {
  interstitialAd = reloadInterstitialAd()
  invokeRequest()
}

func invokeRequest() {
  guard let requestType = requestType else {
    fatalError("No request type specified!")
  }
  switch requestType {
  case .call: call()
  case .text: text()
  }
}

func call() {
  // your code
}

func text() {
  // your code
}