Thanks for your explanations.The following code now seems to work for me in production in combination with “exclude-old-transactions: true":private func expirationDateFromResponse(jsonResponse: NSDictionary) -> Date? {
var latestDate: Date? = nil
if let receiptInfo: NSArray = jsonResponse["latest_receipt_info"] as? NSArray {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
for receipt in receiptInfo {
if let dicReceipt = receipt as? NSDictionary {
if let productID = dicReceipt["product_id"] as? String {
if productID == "[subscription_product_id]" {
if let expirationDate = formatter.date(from: dicReceipt["expires_date"] as! String) {
if latestDate == nil {
latestDate = expirationDate
} else if latestDate! < expirationDate {
latestDate = expirationDate
}
}
}
}
}
}
}
return latestDate
}
Post
Replies
Boosts
Views
Activity
Hi,Thanks for your input!Unfortunately I can't get it to work and have no idea what could be the reason. My app is still crashing when trying to verify the subscription.I use the code below (shortened without catch blocks and if/else optional unwrapping) with a function that iterates over all receipts and picks the one with the latest expiry date.While debugging in Xcode and contacting https://sandbox.itunes.apple.com/verifyReceipt, everything works and it seems that receipts are still returned by Apple in an ordered sequence.Every help is very much appreciated! let receiptData = NSData(contentsOf: Bundle.main.appStoreReceiptURL!)
let receiptToString = receiptData!.base64EncodedString(options: [])
let dict = ["receipt-data" : receiptToString, "password" : "[app store connect shared secret]"]
let request = try JSONSerialization.data(withJSONObject: dict, options: []) as Data
let storeRequest = NSMutableURLRequest(url: URL(string: "https://buy.itunes.apple.com/verifyReceipt"))
storeRequest.httpMethod = "POST"
storeRequest.httpBody = request
let session = URLSession(configuration: URLSessionConfiguration.default)
let dataTask = session.dataTask(with: storeRequest as URLRequest, completionHandler:{(data: Data?,...) -> Void in
let jsonResponse = try (JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary)!
let expDate = self.expirationDateFromResponse(jsonResponse: jsonResponse)
// decide on unlocking subscription based on expDate
})
dataTask.resume()
private func expirationDateFromResponse(jsonResponse: NSDictionary) -> Date? {
var latestDate: Date? = nil
if let receiptInfo: NSArray = jsonResponse["latest_receipt_info"] as? NSArray {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
for receipt in receiptInfo {
if let dicReceipt = receipt as? NSDictionary {
if let expirationDate = formatter.date(from: dicReceipt["expires_date"] as! String) {
if latestDate == nil {
latestDate = expirationDate
} else if latestDate! < expirationDate {
latestDate = expirationDate
}
}
}
}
}
return latestDate
}
Yes, similar issues here!I try to validate, that the user is subscribed to my in-app subscription, upon app launch. Since more than 24 hours (perhaps close to 48) this causes the app to freeze/crash. Seems to happen mostly for users that are actually subscribed. Another issue is that for some users an active subscription is not detected (no crash).There cleary seems something wrong with the https://buy.itunes.apple.com/verifyReceipt server.I reached out to Apple Developer Support. They couldn't help me and told me that it is best to contact technical support, which I haven't so far.This is a very annoying bug concerning revenue and subscription numbers... Please Apple fix it.