2017-05-09 210 views
1

單擊adText按鈕時,我的admob插頁式廣告未顯示。我希望展示插頁式廣告,何時解散以運行我的文字()函數。當我的adCall按鈕被按下時,我希望發生同樣的事情,但插頁式廣告被解僱後運行我的call()函數。爲什麼admob插頁式廣告沒有在iOS上顯示?

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

我不認爲你可以做到這一點。將'text()'函數作爲ViewController初始化或使用委託的屬性傳遞是否解決了您的問題? – Valdmer

回答

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 
}