Zac Zac - 7 months ago 122
Swift Question

Programmatically add AdMob banner

Is there a way to implement adMob banners programmatically so that I can remove them with an in-app purchase?

Something like the iAd:

self.canDisplayBannerAds = true


When there is that line of code the ads are displayed, else the space is used for other views, like a uitableview.

Answer

The following is what I used for my in-app purchasing (IAP), and it works like a charm. It's executed once the IAP is bought.

func removeAds(){
    banner.removeFromSuperview()
    bannerGoogle.removeFromSuperview()
    outRemoveAds.removeFromSuperview()
    restorePurchases.removeFromSuperview()
    let theValue = 10
    NSUserDefaults.standardUserDefaults().setObject(theValue, forKey: "val")
    NSUserDefaults.standardUserDefaults().synchronize()
}

Just create IBOutlets for your ads.