Reputation: 1088
I have implemented interstitial ads from app delegate and it is loading fine. Now I want purchase button to remove it, and I am confused.
here is my code:
class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {
var window: UIWindow?
var myInterstitial: GADInterstitial!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
myInterstitial = createAndLoadInterstitial()
return true
}
func createAndLoadInterstitial()->GADInterstitial {
let interstitial = GADInterstitial(adUnitID: "xxxxx")
interstitial.delegate = self
interstitial.loadRequest(GADRequest())
return interstitial
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
print("interstitialDidReceiveAd")
}
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
print(error.localizedDescription)
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
print("interstitialDidDismissScreen")
myInterstitial = createAndLoadInterstitial()
}
and ViewController:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
override func viewWillAppear(animated: Bool) {
appDelegate.myInterstitial?.presentFromRootViewController(self)
}
Upvotes: 1
Views: 893
Reputation: 8006
Ok, so the real question being - How to remove ads when you purchase it (from the comments).
It can be achieved quite simply - after you receive confirmation that the purchase has succeeded you should do two things :
NSUserDefaults
so that you can know about this in subsequent app launchesmyInterstitial
to nil
, so that no more ads will be shown during this sessionTo sum up with code examples :
func paymentComplete() {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "userAlreadyPaid")
appDelegate.myInterstitial = nil
}
And update your didFinishLaunchingWithOptions
method to something like this :
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let userAlreadyPaid = NSUserDefauls.standardUserDefaults().boolForKey("userAlreadyPaid")
if !userAlreadyPaid {
myInterstitial = createAndLoadInterstitial()
}
return true
}
In order to learn how to implement In App Purchases you can reference the official docs : https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/StoreKitGuide/Introduction.html
Upvotes: 4