我已经从应用程序委托(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()
}
和 View Controller :
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
override func viewWillAppear(animated: Bool) {
appDelegate.myInterstitial?.presentFromRootViewController(self)
}
最佳答案
好的,所以真正的问题是 - 如何在购买时删除广告(来自评论)。
它可以很简单地实现 - 在您收到购买成功的确认后,您应该做两件事:
NSUserDefaults
中设置标志以便您在随后的应用程序启动中了解这一点myInterstitial
至nil
,以便在此 session 期间不再显示广告 总结代码示例:
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
关于ios - 如何从应用代理中删除插页式广告 - 应用内购买,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39310616/