【问题标题】:How to remove interstitial ads from app delegate - in app purchase如何从应用代理中删除插页式广告 - 应用内购买
【发布时间】:2016-09-03 19:30:30
【问题描述】:

我已经从应用代理实现了插页式广告,并且加载正常。现在我想将购买按钮删除,我很困惑。

这是我的代码:

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

和视图控制器:

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

override func viewWillAppear(animated: Bool) {


    appDelegate.myInterstitial?.presentFromRootViewController(self)

}

【问题讨论】:

  • 你对什么感到困惑?这里真正的问题是什么?
  • 购买时如何去除广告?

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


【解决方案1】:

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

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

  • 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
}

要了解如何实现应用内购买,您可以参考官方文档:https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/StoreKitGuide/Introduction.html

【讨论】:

  • if userAlreadyPaiddidFinishLaunchingWithOptions 中应该是 if !userAlreadyPaid,因为在执行 paymentComplete() 之前,Bool 将始终是 false
  • 你完全正确!我一定是打错了——谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
相关资源
最近更新 更多