【问题标题】:SKProduct returns nil the first time, works laterSKProduct 第一次返回 nil,稍后工作
【发布时间】:2019-01-16 01:24:07
【问题描述】:

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

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

【问题讨论】:

    标签: swift in-app-purchase skproduct


    【解决方案1】:

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

    类似

    self.request = SKProductsRequest(....)
    

    【讨论】:

    • 啊,我认为释放是问题所在,那么我将这行代码放在哪里?
    • @AustinM 也许是包含getPurchaseInfo的类?
    • 我就是这么做的。我创建了一个不会被释放的 productsRequest 全局变量,并在 getPurchaseInfo 中实现了它。我认为现在的问题与调用 productsRequest(_ request: SKProductsRequest, didReceieve response: SKProductsResponse) 函数有关,其中请求未使用该变量?
    • 我正在继续发现确切的问题是什么。似乎函数 getPurchaseInfo() 被正确调用,并且当它调用 productsRequest.start() 时因为没有产品而失败。很明显,产品一开始没有定义,我不知道为什么。
    • 我还发现,即使请求成功,该函数每次都会以某种方式抛出错误,在常规请求之后。
    猜你喜欢
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 2015-02-25
    相关资源
    最近更新 更多