When my callButton is clicked it rings a phone number, I want to display my interstitial ad before the call is made. I have tested my ad on the "IBAction function ad" button and it works on the button but when I call it on the callButton func it will not work and goes straight to making a call.
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()
}
}
Of course it goes to making a call. That's what you're telling it to do with UIApplication.shared.open
.
Make the call once the ad has been dismissed in interstitialDidDismissScreen
.
Also check to see if there is an ad to present, interstitialAd.isReady
. If there is no ad to present go straight to making the call.
You're also doing the same thing twice in your 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()
Just call self.interstitialAd = reloadInterstitialAd()
.