swiftin-app-purchasesubscriptionauto-renewable

SwiftUI Auto-Renewable Subscription flow


I have implemented In-App Purchase Auto-Renewable Subscription into my app but I'm not sure if I have done it correctly as the app constantly asks for iTunes login.

In the user case: I am subscribed to monthly payments and my first month is over and expect it to auto-renew.

The flow I have at the moment is as follows...

  1. (in App Delegate) Check receipt is valid

  2. (if receipt IS valid) check all receipts for latest expiration date

  3. (if expired - which seems to be the case after the first month is over) call SKReceiptRefreshRequest to get latest receipts. I have put a count check on this otherwise it gets stuck in a never ending loop.

  4. Do final check to see if latest receipt is in-fact expired.

Is this the correct way to go about this? If not could you shine some light on this?

It all seems to be working fine apart from the annoyance of iTunes login. Which I guess would only be once a month outside of the Sandbox environment which isn't too bad but just want to be sure I'm doing this correctly.

Thanks


Solution

  • Don't call SKReceiptRefreshRequest. This request is usually only for the "restore purchases" mechanism. This is why you're getting the sign in dialogue. StoreKit will return to you as long as you an observer to the payment queue: https://developer.apple.com/documentation/storekit/skpaymentqueue/1506042-add so you should add yourself to the payment queue on app launch. Then keep that object aline to listen for changes for the entire app lifecycle. You will receive the renewals in the updatedTransactions callback https://developer.apple.com/documentation/storekit/skpaymenttransactionobserver/1506107-paymentqueue

    Secondly, you should be doing receipt validation on your own server and not on the client as it will be susceptible to a MITM attack. You can also easily control the logic there and receive Server to server notifications which is best for managing subscriptions.