自动续订IAP仅在App Store上运行时无效

时间:2016-12-31 18:09:07

标签: ios swift in-app-purchase app-store subscription

以下代码应检查用户是否具有有效的自动续订订阅并将其移至下一个屏幕,或者如果无效,则启动IAP流程。 它在使用Xcode的真实设备上运行时效果很好但是当应用程序在应用程序商店中运行时,它不会移动到下一个屏幕或启动IAP。

  • IAP在iTC中获得批准状态
  • IAP共享密钥是正确的
  • 请求网址为"https://sandbox.itunes.apple.com/verifyReceipt"沙箱,"https://buy.itunes.apple.com/verifyReceipt"为实时

if let receiptPath = Bundle.main.appStoreReceiptURL?.path, FileManager.default.fileExists(atPath: receiptPath) {
    print("receipt found")
    let receiptData = NSData(contentsOf: Bundle.main.appStoreReceiptURL!)
    let receiptDictionary = ["receipt-data":receiptData!.base64EncodedString(options: []), "password":"SHARED_SECRET"] as [String : Any]
    let requestData = try! JSONSerialization.data(withJSONObject: receiptDictionary, options: JSONSerialization.WritingOptions.prettyPrinted) 
    print(storeURL)
    let storeRequest = NSMutableURLRequest(url: storeURL as URL)
    storeRequest.httpMethod = "POST"
    storeRequest.httpBody = requestData
    let session = URLSession(configuration: .default)
    session.dataTask(with: storeRequest as URLRequest, completionHandler: { // Validate Receipt
(data, response, connection) -> Void in
        if let jsonResponse: NSDictionary = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary,
            let expirationDate: NSDate = self.formatExpirationDateFromResponse(jsonResponse: jsonResponse) {
            expiryDate = expirationDate as Date
            let days = expiryDate.daysFrom(Date())
            if (days < 0) { // Expired
                DispatchQueue.main.async {
                    self.IAPStart()
                }
            } else {
                DispatchQueue.main.async {
                    self.waitCancelButtonTapped(self)
                    self.howSell()
                }
             }
        }
    }).resume()
} else { // No receipt found
    print("no receipt found")
    IAPStart()
}

0 个答案:

没有答案