swift - SKProduct 第一次返回 nil,稍后工作

标签 swift in-app-purchase skproduct

我的问题很奇怪——我以前从未在论坛上见过它。在请求 IAP (getPurchaseInfo() —> request.start()) 时,我第一次点击它时,它给我一个错误,没有找到任何产品。但是,如果我立即重试,它会接受产品 ID 并允许购买。 Apple 拒绝了我的提交,因为我一开始就有这个错误,而且我不知道如何让它消失。

我必须使用 IAP 的许多功能,这些是我认为可能有用的一些功能。

func getPurchaseInfo() {
        if SKPaymentQueue.canMakePayments() {
            let request = SKProductsRequest(productIdentifiers: NSSet(objects: self.productID) as! Set<String>)
            request.delegate = self
            request.start()
            print("Completed Product Request.")
        }

func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
        var products = response.products
        print(products.count)
        if products.count == 0 {
            //Tell the user there are no products found!
            let prodError = UIAlertController(title: "Error", message: "No products were found.", preferredStyle: .alert)
            let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            prodError.addAction(okAction)
            self.view?.window?.rootViewController?.present(prodError, animated: true, completion: nil)
        }
        else {
            product = products[0]
            print(productID)
        }

我打印了“Completed Product Request”,甚至得到了 1 的 products.count,以及正确的 productID,这意味着有一个产品。但是,我仍然收到一条错误消息(带有我为这种情况设置的 AlertController),告诉我没有产品,如果我删除 AlertController,我会向我显示应用程序崩溃的错误,这意味着找不到该产品. 这是我点击按钮购买 IAP 的功能:

func purchase() {
        SKPaymentQueue.default().add(self)
        self.getPurchaseInfo()
        if self.product == nil {
            let errorPurchasing = UIAlertController(title: "Error", message: "There was an error requesting the purchase. Please try again.", preferredStyle: .alert)
            let okayButton = UIAlertAction(title: "OK", style: .default, handler: nil)
            errorPurchasing.addAction(okayButton)     
            self.view?.window?.rootViewController?.present(errorPurchasing, animated: true, completion: nil)
        }
        else {
            let payment = SKPayment(product: self.product!)
            SKPaymentQueue.default().add(payment)
            if let myP = self.product {
                let payment = SKPayment(product: myP)
                SKPaymentQueue.default().add(payment)
            }
        }
}

当我第一次点击它时,我总是会收到我设置的“购买错误”警报,但在应用程序打开后不会出现。第一次找不到它但第二次找到它是没有意义的。

最佳答案

我建议在实例变量中保留 SKProductsRequest,这样它就不会被释放。

类似的东西

self.request = SKProductsRequest(....)

关于swift - SKProduct 第一次返回 nil,稍后工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54209192/

相关文章:

cocoa - 应用程序购买 Mac Store 中的收据验证问题 "Receipt validation should be presented..."

php - Android InApp 购买验证

swift - 自动续订订阅 SKProduct.PeriodUnit == 7

ios - 我将如何为 SKPaymentQueue 使用未弃用的 API?

html - UITextView html 文本 - 引用项目中的字体

swift - 单击取消、保存草稿或删除草稿时无法关闭邮件 View Controller

iOS - 无法从 Objective-C 访问 Swift Singleton

ios - CNContactViewController 按钮 Action

macos - 在服务器端验证 Mac OS X 的消耗品应用内购买

SKProduct 的 swift 应用内购买问题