Below code is expected to check if user has a valid auto-renewing subscription and move them to next screen OR if invalid, start IAP process. It works great when running on real device with Xcode but when the app is live on app store, it does not move to next screen or start IAP.
- IAP is Approved status in iTC
- IAP shared secret is correct
- request URL is
"https://sandbox.itunes.apple.com/verifyReceipt"
for sand box and"https://buy.itunes.apple.com/verifyReceipt"
for live
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()
}