如何从应用代理中移除插页式广告 - 在应用内购买

时间:2016-09-03 19:30:31

标签: ios swift2 in-app-purchase admob interstitial

我已经从app delegate实施了插页式广告,并且加载正常。现在我想要购买按钮将其删除,我很困惑。

这是我的代码:

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()
}

和ViewController:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

override func viewWillAppear(animated: Bool) {


    appDelegate.myInterstitial?.presentFromRootViewController(self)

}

1 个答案:

答案 0 :(得分:2)

好的,真正的问题是 - 如何在购买时删除广告(来自评论)。

可以非常简单地实现 - 在您收到确认购买成功后,您应该做两件事:

  • NSUserDefaults中设置了一个标记,以便您可以在后续的应用启动中了解这一点
  • myInterstitial设置为nil,以便在此会话期间不再展示广告

总结代码示例:

func paymentComplete() {
    NSUserDefaults.standardUserDefaults().setBool(true, forKey: "userAlreadyPaid")
    appDelegate.myInterstitial = nil
}

并将您的didFinishLaunchingWithOptions方法更新为以下内容:

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 App Purchases,您可以参考官方文档:https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/StoreKitGuide/Introduction.html