Search code examples
iosswiftadmobibactioninterstitial

Why is admob interstitial ad not showing up on iOS?


My admob interstitial ad is not showing when adText button is clicked. I would like the interstitial ad to show and when dismissed to run my text() function. I would like the same thing to happen when my adCall button is pressed but after the interstitial ad is dismissed to run my call() function.

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

Solution

  • As I understand you correctly, you just want to invoke the actual behavior of a button after the ad has been presented and dismissed. In this case, just save the request type, present the ad, dismiss the ad, reload the interstitial and call the request of the saved request type.

    First, define the types:

    enum RequestType {
      case call
      case text
    }
    

    Second, update your view controller:

    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
    }