1

我正在尝试在我的游戏中集成一个非常基本的单一 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")在首次购买后显示 - 所以我很难看到出了什么问题。

4

2 回答 2

0

好的,所以我想出了答案,我的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
}

希望这对将来有同样问题的人有所帮助!

于 2021-09-28T09:43:49.997 回答
0

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

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

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

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

于 2021-09-27T12:03:13.700 回答