Swift 如何在 IAP 后隐藏插页式广告?
How do I hide Interstitial ads after IAP in Swift?
我已经尝试用我的广告横幅视图做到这一点并且效果很好,但我真的不知道如何为我的插页式广告做到这一点。
我不是那么熟练所以请尝试以非常简单易懂的方式解释它,我的选择是什么。
我的横幅广告查看非常简单
if UserDefaults.standard.getActiveRemoveAdsSubscription() == true {
bannerView.isHidden = true
} else if UserDefaults.standard.getActiveRemoveAdsSubscription() == true {
bannerView.isHidden = true
} else {
bannerView.isHidden = false
}
但是我该如何处理不是“isHidden”成员的 InterstitalAd?
这是我的 InterstitalAd 代码:
import UIKit
import GoogleMobileAds
class UITabBarViewController: UITabBarController, GADFullScreenContentDelegate {
var interstitial: GADInterstitialAd!
let request = GADRequest()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if UserDefaults.standard.getActiveRemoveAdsSubscription() == true {
} else if UserDefaults.standard.getActiveRemoveAdsSubscription() == true {
} else {
}
_ = Timer.scheduledTimer(timeInterval: 30.0, target: self, selector: #selector(timerBlock), userInfo: nil, repeats: true)
}
@objc func timerBlock()
{
print("within the timer!")
// Active link
// GADInterstitialAd.load(withAdUnitID: "x", request: request, completionHandler: {ad, error
// Test Link
GADInterstitialAd.load(withAdUnitID: "ca-app-pub-x", request: request, completionHandler: {ad, error
in
if let error = error {
print("Failed to load the ad: \(error)")
return
}
self.interstitial = ad
self.interstitial.fullScreenContentDelegate = self
})
if interstitial != nil{
interstitial!.present(fromRootViewController: self)
}
else
{
print("Ad didn't load / Wasn't ready")
}
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("Error: \(error.localizedDescription)")
}
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Success!!")
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("User dismissed the ad")
}
有 2 种方案可以处理此问题。
- 购买后立即删除
GADInterstitialAd
。
- 检查广告是否在应用启动时被移除。
在这两种情况下,让您的插页式广告可为空,例如:
private var interstitial: GADInterstitialAd?
处理第一种情况:
if Purchase.isSuccesful == true {
interstitial = nil
}
处理第二种情况(假设你是在本地保存购买状态):
在 timerBlock()
函数中,只需检查广告是否已删除并相应地加载广告,例如:
@objc func timerBlock() {
if UserDefaults.standard.getActiveRemoveAdsSubscription() == false {
// Load Ad & show Interstitial
} else {
print("Ad Removed, not loading Interstitial.")
}
}
我已经尝试用我的广告横幅视图做到这一点并且效果很好,但我真的不知道如何为我的插页式广告做到这一点。
我不是那么熟练所以请尝试以非常简单易懂的方式解释它,我的选择是什么。
我的横幅广告查看非常简单
if UserDefaults.standard.getActiveRemoveAdsSubscription() == true {
bannerView.isHidden = true
} else if UserDefaults.standard.getActiveRemoveAdsSubscription() == true {
bannerView.isHidden = true
} else {
bannerView.isHidden = false
}
但是我该如何处理不是“isHidden”成员的 InterstitalAd?
这是我的 InterstitalAd 代码:
import UIKit
import GoogleMobileAds
class UITabBarViewController: UITabBarController, GADFullScreenContentDelegate {
var interstitial: GADInterstitialAd!
let request = GADRequest()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if UserDefaults.standard.getActiveRemoveAdsSubscription() == true {
} else if UserDefaults.standard.getActiveRemoveAdsSubscription() == true {
} else {
}
_ = Timer.scheduledTimer(timeInterval: 30.0, target: self, selector: #selector(timerBlock), userInfo: nil, repeats: true)
}
@objc func timerBlock()
{
print("within the timer!")
// Active link
// GADInterstitialAd.load(withAdUnitID: "x", request: request, completionHandler: {ad, error
// Test Link
GADInterstitialAd.load(withAdUnitID: "ca-app-pub-x", request: request, completionHandler: {ad, error
in
if let error = error {
print("Failed to load the ad: \(error)")
return
}
self.interstitial = ad
self.interstitial.fullScreenContentDelegate = self
})
if interstitial != nil{
interstitial!.present(fromRootViewController: self)
}
else
{
print("Ad didn't load / Wasn't ready")
}
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("Error: \(error.localizedDescription)")
}
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Success!!")
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("User dismissed the ad")
}
有 2 种方案可以处理此问题。
- 购买后立即删除
GADInterstitialAd
。 - 检查广告是否在应用启动时被移除。
在这两种情况下,让您的插页式广告可为空,例如:
private var interstitial: GADInterstitialAd?
处理第一种情况:
if Purchase.isSuccesful == true {
interstitial = nil
}
处理第二种情况(假设你是在本地保存购买状态):
在 timerBlock()
函数中,只需检查广告是否已删除并相应地加载广告,例如:
@objc func timerBlock() {
if UserDefaults.standard.getActiveRemoveAdsSubscription() == false {
// Load Ad & show Interstitial
} else {
print("Ad Removed, not loading Interstitial.")
}
}