【问题标题】:SwiftyStoreKit IAP Purchasing but not remembering it's settings after launchSwiftyStoreKit IAP 购买但启动后不记得它的设置
【发布时间】:2021-11-19 13:40:48
【问题描述】:

我正在尝试在我的游戏中集成一个非常基本的单一 IAP,我在这里通过我的GameScene 拨打电话

 let alert = UIAlertController(title: "Upgrade", message: "Would you like to remove ads?", preferredStyle: .alert)

                alert.addAction(UIAlertAction(title: "Remove Ads", style: .default, handler: { action in
                    print("Pressed Remove Ads")
                    
                    GameViewController().buytheIAP()
                    
                }))
                
                alert.addAction(UIAlertAction(title: "Restore Purchases", style: .default, handler: { action in
                    print("Pressed Restore")
                    GameViewController().restoretheIAP()
                }))
                
                alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { action in
                    print("Pressed Cancel")
                }))
              
                view?.window?.rootViewController?.present(alert, animated: true, completion: nil)
                      }

那些方法调用正确,在GameViewController.swift里面引用这些;

func buytheIAP(){
         
         iAPHelper.purchase()
         print("OK Lets upgrade")
    }
    
    func restoretheIAP(){
        
         iAPHelper.restorePurchase()
         print("OK Lets restore")
    }
    
    func restoreDidSucceed() {
     
        UserDefaults.setValue(true, forKey: iAPHelper.productID)
        //this should have something like hide banner etc.
         bannerView.isHidden = true
    }
    
    func purchaseDidSucceed() {
        UserDefaults.setValue(true, forKey: iAPHelper.productID)
        //as above this should have something like hide banner etc.
         
         bannerView.isHidden = true
         
         print("Purchased upgrade ENJOYYYYYYYY")
         
    }
    
    func nothingToRestore() {
        
    }
    
    func paymentCancelled() {
        
    }

测试 IAP 通过,它从应用商店获取正确的信息,我使用我的沙盒详细信息进行购买,它正确通过并显示成功购买消息。但是bannerView并没有隐藏,更重要的是,再次重启游戏后,一切都忘记了,游戏认为什么都没有购买。我猜它一定是缺少某种检查。

我的viewDidLoad有这个

 if userDefaults.bool(forKey: iAPHelper.productID) {
                   
              bannerView.isHidden = true
              print("It is purchased, so DO NOT show the ads")
         } else{
         
         bannerView.adSize = getAdaptiveSize()
         bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
         bannerView.delegate = self
         bannerView.rootViewController = self
         bannerView.load(GADRequest())
         addBannerViewToView(bannerView)
              
              print("Not purchased, so show the ads")
              
         }

它总是显示print("Not purchased, so show the ads")

IAPHelper 文件,用于购买;

 func purchase() {
        
        SwiftyStoreKit.purchaseProduct(productID, quantity: 1, atomically: true) { [self] result in
            switch result {
            case .success:
                
                delegate?.purchaseDidSucceed()
                print("OK It's purchased")
                
            case .error(let error):
                switch error.code {
                case .unknown: print("Unknown error. Please contact support")
                case .clientInvalid: print("Not allowed to make the payment")
                case .paymentCancelled:
                    
                    delegate?.paymentCancelled()
                    
                case .paymentInvalid: print("The purchase identifier was invalid")
                case .paymentNotAllowed: print("The device is not allowed to make the payment")
                case .storeProductNotAvailable: print("The product is not available in the current storefront")
                case .cloudServicePermissionDenied: print("Access to cloud service information is not allowed")
                case .cloudServiceNetworkConnectionFailed: print("Could not connect to the network")
                case .cloudServiceRevoked: print("User has revoked permission to use this cloud service")
                default: print((error as NSError).localizedDescription)
                    
                }
            }
        }
    }
}

并且在初次购买后日志确实显示print("OK It's purchased") - 所以我很难知道出了什么问题。

【问题讨论】:

    标签: ios swift in-app-purchase swiftystorekit


    【解决方案1】:

    IAP 委托函数不会(保证会)在 UI/主线程上调用,这就是您的视图不会隐藏的原因。

    您没有看到一些 iOS 警告说您尝试在非主线程上设置 UIView 属性吗?

    您的自定义购买信息未保存在UserDefaults 中的事实可能是由于从 Xcode 中过早地终止了该应用。

    UserDefaults 确实会保存在正常应用流程中设置的内容。)

    【讨论】:

    • 没有关于 UIView 的警告 - 我确实让应用程序运行了一段时间,然后再次关闭它进行测试,这很奇怪 - 我也许可以添加一个观察者来发送隐藏横幅消息,但是还是不能解决忘记UserDefaults中设置的问题?
    • @DannyRoy 只是设置了一个断点,看看你在哪个线程中隐藏了这个视图。
    • 有趣——似乎func purchaseDidSucceed() { } 即使在成功购买后也没有被调用——所以由于某种原因委托方法没有启动。这就解释了为什么 UserDefaults 没有被保存而 bannerView 没有隐藏。
    【解决方案2】:

    好的,所以我想出了答案,我的AppDelegate.swift 部分didFinishLaunchingWithOptions 中缺少一行

    UserDefaults.standard.register(defaults: ["adRemove" : false])
    

    我重命名了userDefaults.bool(forKey: iAPHelper.productID) 以使其更易于使用/理解。所以在我原来的帖子中已经被UserDefaults.standard.register(defaults: ["adRemove" : false]) 取代了。

    然后你可以在任何地方使用它来检查,例如;

    if !userDefaults.bool(forKey: "adRemove") {
    // Do something here
    }
    

    希望这对以后有同样问题的人有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-15
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多