Skip to content

Instantly share code, notes, and snippets.

@ren6
Created June 17, 2019 14:31
Show Gist options
  • Save ren6/066ee05603dc2870994968596bb12c9c to your computer and use it in GitHub Desktop.
Save ren6/066ee05603dc2870994968596bb12c9c to your computer and use it in GitHub Desktop.
Parse Apple receipt example
private func parseReceipt(_ json : Dictionary<String, Any>) {
// It's the most simple way to get latest expiration date. Consider this code as for learning purposes. Do not use current code in production apps.
guard let receipts_array = json["latest_receipt_info"] as? [Dictionary<String, Any>] else {
self.refreshSubscriptionFailureBlock?(nil)
self.cleanUpRefeshReceiptBlocks()
return
}
for receipt in receipts_array {
let productID = receipt["product_id"] as! String
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
if let date = formatter.date(from: receipt["expires_date"] as! String) {
if date > Date() {
// do not save expired date to user defaults to avoid overwriting with expired date
UserDefaults.standard.set(date, forKey: productID)
}
}
}
self.refreshSubscriptionSuccessBlock?()
self.cleanUpRefeshReceiptBlocks()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment