Search code examples
iosswiftin-app-purchaseiad

Remove all iAds app wide?


I've added in app purchases to remove iAds for my iOS app.

I've put this in the logic once the removeAds product is bought (which definitely triggers):

func removeAds() {
    defaults.setObject("True", forKey: "remove_adverts")
    self.canDisplayBannerAds = false
    self.removeAdButton.enabled = false
    println("removed")
}

And I've put this at the top of each controller to handle it.

if let showAds = defaults.dataForKey("remove_adverts") {
    self.canDisplayBannerAds = false
    self.removeAdButton.enabled = false
    println("Ads shouldn't show")
} else {
    self.canDisplayBannerAds = true
}

But the ads still show.

Is there a better way to do this?


Solution

  • When you're checking to see if the user has purchased the IAP to remove ads in this line if let showAds = defaults.dataForKey("remove_adverts") you're checking for dataForKey when you should be checking for objectForKey because you used setObject when setting your default value. Alternatively, you could use setBool so your code would look something like this:

    func removeAds() {
        defaults.setBool(true, forKey: "removeAdsPurchased")
        self.canDisplayBannerAds = false
        println("iAd removed and default value set")
        defaults.synchronize()
    }
    

    and

        let showAds = defaults.boolForKey("removeAdsPurchased")
        if (showAds) {
            // User purchsed IAP
            // Lets remove ads
            self.canDisplayBannerAds = false
            println("iAd removed")
        } else {
            // IAP not purchased
            // Lets show some ads
            self.canDisplayBannerAds = true
            println("Showing iAd")
        }