在 IBAction 按钮上加载插页式广告
load interstitial ad on IBAction button
当我的 callButton 被点击时,它会响起一个 phone 号码,我想在拨打电话之前展示我的插页式广告。我已经在 "IBAction function ad" 按钮上测试了我的广告,它可以在按钮上运行,但是当我在 callButton 函数上调用它时,它不会运行并直接拨打电话。
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()
}
}
我认为您的问题与 present
方法在插页式广告上的同步有关。我不知道实现是什么,但似乎广告的展示并没有阻止调用它的线程。您应该尝试实现 GADInterstitialDelegate
以在显示广告时获取事件,然后继续从那里拨打电话。
当然是去打电话了。这就是你告诉它用 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()
。
试试这个:
GADInterstitial is a one time use object. That means once an
interstitial is shown, hasBeenUsed returns true and the interstitial
can't be used to load another ad. To request another interstitial,
you'll need to create a new GADInterstitial object. The best practice,
as shown above, is to have a helper method to handle creating and
loading an interstitial.
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)
}
}
当我的 callButton 被点击时,它会响起一个 phone 号码,我想在拨打电话之前展示我的插页式广告。我已经在 "IBAction function ad" 按钮上测试了我的广告,它可以在按钮上运行,但是当我在 callButton 函数上调用它时,它不会运行并直接拨打电话。
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()
}
}
我认为您的问题与 present
方法在插页式广告上的同步有关。我不知道实现是什么,但似乎广告的展示并没有阻止调用它的线程。您应该尝试实现 GADInterstitialDelegate
以在显示广告时获取事件,然后继续从那里拨打电话。
当然是去打电话了。这就是你告诉它用 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()
。
试试这个:
GADInterstitial is a one time use object. That means once an interstitial is shown, hasBeenUsed returns true and the interstitial can't be used to load another ad. To request another interstitial, you'll need to create a new GADInterstitial object. The best practice, as shown above, is to have a helper method to handle creating and loading an interstitial.
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)
}
}