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

标签 ios swift2 in-app-purchase admob interstitial

我已经从应用程序委托(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 中设置标志以便您在随后的应用程序启动中了解这一点
  • 设置 myInterstitialnil ,以便在此 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/

    相关文章:

    ios - 状态栏使用 Capacitor 覆盖 iOS 上的 webview

    ios - xcodebuild 无法连接到模拟器

    ios - 如何在 iOS 中获取 iPod 库中歌曲的文件位置

    ios - 如何在 didReceiveRemoteNotification 中获取 userInfo JSON 值

    ios - 在应用程序内购买。已发送但无法接收的产品 ID

    ios - 应用审查时应用内购买失败

    c# - 未调用 UITableviewCell GetCell

    ios - UIImageView 方面适合和居中

    swift - 快速保存按钮图像的状态

    macos - Mac StoreKit “An unknown error has occured”购买