There are many ways to integrate auto-renewable subscriptions or regular in-app purchases into your Swift/SwiftUI project.
In this tutorial, I will show you one of the easiest ways to do it using the PurchaseKit framework.
NOTE: You can get the full PurchaseKit framework from the official Apps4World website: https://apps4world.com/in-app-purchases-swift.html but for this tutorial, you can download a limited version from here.
For 10% OFF - use coupon code ZWV-YJN-TVZ
Drag & Drop the PurchaseKit folder into your Xcode project.
You must Embed & Sign the PurchaseKit framework
App Store Connect -> In-App Purchases -> App-Specific Shared Secret
If you’re using Swift then open the AppDelegate.swift and if you’re using SwiftUI, then open 'projectName'App.swift (or SceneDelegate). In my example it’s going to be the MediumSubscriptionsApp.swift file.
Import the PurchaseKit and add these 2 lines of code. One to configure the framework with your app-specific shared secret, which is required by Apple in order to fetch the subscription receipt. The second line is required to fetch all of your in-app purchases with given product identifiers.
You have to add the line 2, 7 and 8
On line 8 we initialize the PKDiamonView UI template
You can present this SwiftUI SubscriptionFlow view in many ways. Also, if you want to present this in a Swift project, just call this method: PKManager.present(theme: AnyView(SubscriptionFlow), fromController: self) where you pass the UI theme in an AnyView wrapper, then the controller that is presenting this UI theme.
Another critical part of auto-renewable subscriptions is to verify subscription receipt. We do this as soon as the PurchaseKit is configured, ideally when the first screen of the app is visible, in my example I call this function onAppear or for Swift projects, in viewDidLoad . See the entire SwiftUI file below.
See line 22 where we’re verifying the subscription receipt for each product identifier
Q) Where do I configure the price or duration for a subscription?
A) Pricing, duration, title are managed on the App Store Connect. The PurchaseKit framework takes care of fetching this data and formatting it properly for you, so each UI template will render the price, duration properly. But you can also call the PKManager.subscriptionPeriod to get back to duration (ex: 7) or unit (ex: days) for a specific product identifier.
Q) What about free trials?
A) Yes, the UI templates handle that as well. You can also call this PKManager.introductoryPrice yourself to see if a given productId offers introductory pricing from the App Store Connect.
Q) Is there any servers/backend systems involved?
A) No backed/servers are required, however, please read Apple’s documentation, because they are encouraging developers to use server-side validation, which is a more complex method compared to simply calling Apple’s verifyReceipt API. This framework works great if you have some simple use cases, where you don’t need the additional cost on maintaining a server.
Q) Are there any limitations?
A) This framework doesn’t support Non-Renewing Subscriptions, because we don’t get a receipt from Apple for this type of subscription. To support such subscriptions, you have to integrate a backend system to keep track of user’s receipts then validate them via your system.
If you have any questions or suggestions, please feel free to send an email to email@example.com
Thanks for reading!