从堆栈溢出上的所有其他答案中,我没有找到此特定问题的答案。

我正在使用SKReceiptRefreshRequest还原购买。我正在使用沙箱帐户。我有以下代码:

let request = SKReceiptRefreshRequest()

request.delegate = self

request.start()

结果是调用SKRequestDelegate协议的requestDidFinish时。我返回的请求的receiveProperties属性为nil。我该怎么解释?文档(Refreshing the App Receipt)说要检查收据,但似乎没有要检查的东西。

这是我的代码,当我假设以零净值的回执来获取请求意味着我收到了回执时。
extension SettingsTableViewController: SKRequestDelegate {

    func requestDidFinish(_ request: SKRequest) {

        print("requestDidFinish")

        print("request=", request)

        if let receiptRefreshRequest = request as? SKReceiptRefreshRequest {

            print("receipt properties=", receiptRefreshRequest.receiptProperties as Any)

        }

        if iCloudAvailable() {

            ubiquitousKeyValueStore.set(true, forKey: UbiquitousKeys.iMessageExtension)

            let alertMessage = "iMessage Saved Messages has been restored."

            let alert = UIAlertController(title: nil, message: alertMessage, preferredStyle: .alert)

            let actionOK = UIAlertAction(title: "OK", style: .cancel, handler: nil)

            alert.addAction(actionOK)

            present(alert, animated: true, completion: nil)

        }

    }

    func request(_ request: SKRequest, didFailWithError error: Error) {

        print("requst(_:didFailWithError:)")

        if let receiptRefreshRequest = request as? SKReceiptRefreshRequest {

            print("receipt properties=", receiptRefreshRequest.receiptProperties as Any)

        } else {

            print("request=", request)
        }

        print("error=", error)

        let alertMessage = "There are no purchases to restore."

        let alert = UIAlertController(title: nil, message: alertMessage, preferredStyle: .alert)

        let actionOK = UIAlertAction(title: "OK", style: .cancel, handler: nil)

        alert.addAction(actionOK)

        present(alert, animated: true, completion: nil)

    }

}

最佳答案

我找到了该网页的答案。苹果的文档没有在需要的地方显示。

"Restoring non-consumable IAPs in iOS" at upbeat.it

func validateReceipt() {
    let recURL = Bundle.main.appStoreReceiptURL!
    let contents = NSData(contentsOf: recURL)
    let receiptData = contents!.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
    print(receiptData)
    let requestContents = ["receipt-data" : receiptData]
    print(requestContents)
    let requestData = try? JSONSerialization.data(withJSONObject: requestContents, options: [])
    print(requestData)
    let serverURL = "https://sandbox.itunes.apple.com/verifyReceipt" // TODO:change this in production with https://buy.itunes.apple.com/verifyReceipt
    let url = NSURL(string: serverURL)
    let request = NSMutableURLRequest(url: url! as URL)
    request.httpMethod = "POST"
    request.httpBody = requestData
    let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in
        guard let data = data, error == nil else {
            self.notifyReceiptResult(false)
            return
        }
        do {
            let json = try JSONSerialization.jsonObject(with: data, options:[]) as? [String: Any]
            if let receipt = json?["receipt"] as? [String: AnyObject],
                let inApp = receipt["in_app"] as? [AnyObject] {
                print(inApp)
                if (inApp.count > 0) {
                    self.notifyReceiptResult(true)
                } else {
                    self.notifyReceiptResult(false)
                }
            }
        }
        catch let error as NSError {
            print(error)
            self.notifyReceiptResult(false)
        }
    })
    task.resume()
}

08-04 01:46