ios - 在应用内购买后删除 View Controller 上的所有广告

标签 ios swift in-app-purchase

此过程分为两部分,In App Purchase to Remove AdsIn App Purchase 后移除所有 View 上的所有广告。我已经完成了 In App Purchase to Remove Ads,当我用我的 Sandbox Tester 测试它时,它可以正常工作。我需要有关在 App Purchase 中实现 Removing all Ads on all view 的建议。

我尝试过的选项是通过为我的 bool 值使用 static 来实现一个全局变量,它可以在其他 View Controller 中使用。这些只是我实现这些功能的代码的一部分。

RemoveAdsViewController.swift

 static var adRemovalPurchased = false

 @IBAction func removeAdButton(_ sender: Any) {
    purchase(purchase: adRemoval)

}

    func purchase(purchase: RegisteredPurchase){
    NetworkActivityIndicatorManager.NetworkOperationStarted()
    SwiftyStoreKit.purchaseProduct(bundleID + "." + purchase.rawValue, completion:{
        result in
        NetworkActivityIndicatorManager.networkOperationFinished()
        if case .success(let product) = result {
            if product.needsFinishTransaction{
                SwiftyStoreKit.finishTransaction(product.transaction)
                RemoveAdsViewController.adRemovalPurchased = true
                print("Turning Banner off")
            }
            self.showAlert(alert: self.alertForPurchaseResult(result: result))
        }

    })

}

DifferentViewController.swift

 override func viewDidLoad() {
    super.viewDidLoad()
if (RemoveAdsViewController.adRemovalPurchased == true) {
        bannerView.isHidden = true
        print("There is no banner")
    } else {
        bannerView.rootViewController = self
        bannerView.load(GADRequest())
        print("There is a banner!")
    }
}

请注意,所有代码均有效。我用我的沙盒测试器购买了 removeAds,它应该给出 adRemovalPurchase = true 的 bool 值。但它只是没有识别 DifferentViewController.swift 中 bool 值的值,它仍在显示广告。

我正在使用 SwiftyStoreKit。

感谢您的宝贵时间!谢谢。

最佳答案

RemoveAdsViewController.swift

 func purchase(purchase: RegisteredPurchase){
    NetworkActivityIndicatorManager.NetworkOperationStarted()
    SwiftyStoreKit.purchaseProduct(bundleID + "." + purchase.rawValue, completion:{
        result in
        NetworkActivityIndicatorManager.networkOperationFinished()
        if case .success(let product) = result {
            if product.needsFinishTransaction{
                SwiftyStoreKit.finishTransaction(product.transaction)
            }

            self.showAlert(alert: self.alertForPurchaseResult(result: result))
        }
        let adRemovalPurchased = UserDefaults.standard
        adRemovalPurchased.set(true, forKey: "adRemoved")
        adRemovalPurchased.synchronize()
        print (adRemovalPurchased.bool(forKey: "adRemoved"))
        print("Turning Banner off")

    })

}

DifferentViewController.swift

  let adRemovalPurchased = UserDefaults.standard

  if !adRemovalPurchased.bool(forKey: "adRemoved") {
        bannerView.rootViewController = self
        bannerView.load(GADRequest())
        print("There is a banner!")
    } else {
        bannerView.isHidden = true
        print("There is no banner")
    }

我重新审视了这个问题,发现您需要在所有 View Controller 中声明 let adRemovalPurchased = UserDefaults.standard 变量。此代码有效!

关于ios - 在应用内购买后删除 View Controller 上的所有广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41582498/

相关文章:

android - 到目前为止,React Native 与 WPA 应用程序的体验

ios - friend 列表和 friend 的个人资料图片 - Facebook Graph API(在 Swift 中)

iOS 操作时出现 JSON 错误

ios - CIImage 应该是平等的吗?

ios - 应用内购买以填充核心数据

ios - 导航栏进入困惑状态

ios - 联系人信息样式 TableView

ios - 无法将数据获取到 MasterViewController 中的 UITableView 中

iphone - 在 iphone 应用程序的 InApp 购买中检索产品列表时出现问题

ios - 应用内购买,SKProductsRequest 返回 0 - 产品仍在审核中