Swift & SwiftUI Auto-Renewable subscriptions in 3 steps
There are many ways to integrate auto-renewable subscriptions or regular in-app purchases into your Swift/SwiftUI project.
In this blog post, 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.
1) Drag & Drop the downloaded PurchaseKit into your project
After you’ve downloaded the limited (demo) version of the PurchaseKit framework + UI templates. All you have to do is drag & drop this folder into your Xcode project. Make sure to ‘Embed & Sign
’ the PurchaseKit framework.
2) Configure the PurchaseKit with your in-app purchase product identifier
At this step, your goal is to configure the PurchaseKit framework with your in-app purchase product identifiers and app secret.
To generate your App-Specific Share Secret, go to your App Store Connect account, select your app then Manage under In-App Purchases section.
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.
3) Prepare and Present the in-app purchase UI flow
This is the most interesting step, where you get to pick one of the 4 UI templates to present in your app. You can skip this step if you don’t want to use any of these 4 templates, you can simply create your own, then call purchaseProduct
method on the PurchaseKit framework. Also, restorePurchases
method is available as well.
For this example, I will use the template with the orange diamond header. Since all these templates are built with SwiftUI, I will simply initialize the UI template with the title, subtitle, features that this template will unlock, product identifiers, and a completion block.
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.
FAQ:
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.
Video Demo: https://www.youtube.com/watch?v=Vqw4DfdzAoE
PDF Guide: https://apps4world.com/purchasekit.pdf
If you have any questions or suggestions, please feel free to send an email to support@apps4world.com
Thanks for reading!