问题
After buying a product from in-app purchase (Auto-renewable), I hit API on server to give buying information.
How to know that product is re-new for hitting API ?
When user cancel that subscription, how to known that ?
回答1:
Swift 3: In order for you to detect when the user has cancelled the subscription you need to do the following;
- Download the app receipt
- Validate the receipt so you can get the json back containing all the in-app purchases and subscriptions dictionaries
Now inside each receipt there is a field in the dictionary called cancellation_date if this is subscription purchase and otherwise not available for other in-app purchases. If this is nil then there's no cancellation occurred, but if this has a value which contains the cancellation date then a cancellation did occurred and according to apple:
Cancellation Date For a transaction that was canceled by Apple customer support, the time and date of the cancellation.
Treat a canceled receipt the same as if no purchase had ever been made.
then link below explains all the fields you can use inside the receipts;
https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html
Code Example:
// Create receipt request
let receiptRefreshRequest = SKReceiptRefreshRequest()
// Get the receiptUrl from the main bundle
let receiptUrl = Bundle.main().appStoreReceiptURL
//If the receipt file exist on local device
if (receiptUrl as NSURL?)?.checkResourceIsReachableAndReturnError(nil) == true{
// Get the file as data
let receipt: Data = try! Data(contentsOf: receiptUrl!)
}
now you send the receipt to apple server to validate it using your server as apple recommend. After you get callback from the validation you check the cancellation date.
回答2:
Apple does not provide anything built into iOS or a REST API that gives you simple subscription details, nor are there any callbacks that you can listen for and respond to in regards to renewal or cancellation. Apple does have an API that, when given a user's local receipt and a “shared secret” generated in iTunes Connect, returns a JSON object of the user's purchase history for your app, including their current subscription information. More Information check this link
来源:https://stackoverflow.com/questions/37944917/auto-renewable-subscription-for-in-app-purchase