SwiftUI, In-App Purchases and RevenueCat
How to implement in app purchases & subscription in a SwiftUI application
In my latest open source SwiftUI project, AC Helper (an Animal Crossing companion application) that I released on the App Store, I’ve decided to add a monthly and yearly subscription alongside a one time lifetime in app purchase.
You can find the source of the project here:
You can now download the app on the App Store! This is a full featured Animal Crossing helper application written…
And download the application on the App Store here:
AC Helper is a companion app for Animal Crossing: New Horizons. You'll get access to all the information you need to…
Initially I wanted to implement the in app purchase myself using
StoreKit, and I actually had it working, but for subscriptions I had to handle so many edge cases, check the receipt etc…(which should all be built in feature of
StoreKit). And I’ve also I’ve heard so many good words about RevenueCat that I had to try it. So I deleted my implementation and implemented their SDK.
They have a very straightforward framework and you can add it using the Swift Package Manager. A good news because in AC Helper I don’t want to use any third party dependency manager. Once added you just have to import Purchases and you can start implementing it right away. You’ll still have to reference your in app purchases on the admin area on their website, but it’s simple and well documented enough that I won’t detail it here.
In order to make it work I’ve decided to make an
SubscriptionManager. I’ll later inject this object as an
EnvironmentObject to be able to retrieve and observe it from any views. Here is the code: