当我不包括在应用程序内购买的测试广告工作正常。我点击的细胞,它需要我到下一个视图控制器,以及广告弹出。然而,当我包括应用内购买的广告不会出现,即使用户没有付费的广告移除。
该广告出现与此功能:
func showAd() {
self.interstitial = createInterstitialAd()
}
但是,当我添加此,广告不能使用,即使用户没有支付给删除广告展示。
func showAd() {
if let purchased = UserDefaults.standard.value(forKey: "payment") as? Bool{
if purchased == true{
interstitial = nil
print("there is no ad!!!!")
}else{
self.interstitial = createInterstitialAd()
print("there is an ad!!!")
}
}
你的问题是,最初会出现在UserDefaults
支付键的值。这将导致外if
声明落空,导致没有广告。
你可以让你的代码使用bool(forKey:)
简单 - 这将返回false
其中关键是不存在UserDefaults
而非nil
:
func showAd() {
if UserDefaults.standard.bool(forKey: "payment") {
interstitial = nil
print("there is no ad!!!!")
} else {
self.interstitial = createInterstitialAd()
print("there is an ad!!!")
}
}