Reputation: 123
The test ad works fine when I don't include the in app purchase. I click on the cell, it takes me to the next view controller, and the ad pops up. However, when I include in-app purchases the ad doesn't show up even if the user didn't pay to remove ads.
The ad shows up with this function:
func showAd() {
self.interstitial = createInterstitialAd()
}
But when I add this, the ad doesn't show even if the user hasn't paid to remove ads.
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!!!")
}
}
Upvotes: 0
Views: 79
Reputation: 114855
Your problem is that initially there will be no value in UserDefaults
for the payment key. This will cause the outer if
statement to fall through, resulting in no ad.
You can make your code simpler by using bool(forKey:)
- This will return false
where the key is not present in UserDefaults
rather than 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!!!")
}
}
Upvotes: 1