1

我的问题很奇怪——我以前从未在论坛上看到过。在我第一次单击它时请求 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)
        }

我打印了“完成的产品请求”,甚至得到了 products.count 1,以及正确的 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)
            }
        }
}

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

4

1 回答 1

1

我建议保留SKProductsRequestin 实例变量,以免它被释放。

所以像

self.request = SKProductsRequest(....)
于 2019-01-16T01:26:21.897 回答