In-App Purchase Tutorial: Auto-Renewable Subscriptions
Auto-renewable subscriptions provide a way of offering your users continuous access to your app’s renewing content – providing them with a great user experience and access to content they care about, and you with an appealing business model. By Rony Rozen.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
In-App Purchase Tutorial: Auto-Renewable Subscriptions
25 mins
Subscription Groups
You encountered the concept of a subscription group when you were setting up your auto-renewable subscription on App Store Connect. As mentioned, this is basically a group of subscription products, but there’s a bit more to it than that.
You already know that each subscription must be a part of a subscription group. Users can select the subscription option that works best for them based on the price and duration of each.
What you may not know is that users can only buy one subscription within a group at a time. This is great if you want to prevent your users from accidentally purchasing multiple subscriptions that grant access to the same content. All you need to do to prevent this case is use a single subscription group for all relevant subscription products.
If you do want to offer multiple subscriptions at the same time, with each granting access to a different kind of content, you should use multiple subscription groups. Make sure that your users understand that they are actually subscribing to multiple services and don’t expect only a single active subscription. This may lead to frustrated users who cancel subscriptions and leave negative reviews, which in turn may prevent other users from subscribing, and nobody wants that. :]
To prevent these kinds of situations, make sure your subscription options are clear and easy to understand. The name of your subscription group is only visible to you, not to your users, so use it in a way that makes the most sense to you.
You can rank you subscriptions within each group. The ones that offer access to the most content, features or services, regardless of duration, should be at the top. You can have multiple subscriptions at each level if the offerings are equal. You set this up by dragging the subscriptions within the group. Drag one on top of the other to set them at the same level.
Users can choose to upgrade, crossgrade or downgrade their subscription in their account’s subscription page on the App Store:
- When a user upgrades, the transition takes effect immediately and they receive a refund of the prorated amount of their original subscription.
- When a user downgrades, the original subscription continues until the next renewal date and is then renewed at the lower level.
- When a user crossgrades, if the subscriptions are the same duration, the new subscription begins immediately. Otherwise, the new subscription goes into effect at the next renewal date.
Adding More Subscriptions
You’re now going to add another auto-renewable subscription to the app: yearly. Since the app only offers one type of content — life changing quotes by Winnie the Pooh — you’ll keep the new auto-renewable subscription at the same level as the existing subscription.
Go to App Store Connect and add another auto-renewable subscription product. When prompted to choose the subscription group for your new product, choose the same PoohWisdomSubs group you created earlier.
Set the subscription Duration field to 1 Year and fill out all of the other necessary information. Click Save and confirm the status is Ready to Submit. Copy the Product ID for your new auto-renewable subscription. You’ll need it shortly.
Now, go back to Xcode.
Open Main.storyboard and change the Purchase button title to Subscriptions, because you’re going to change the button to give the user a choice between the two available auto-renewable subscription options.
Go to ViewController.swift, locate displayRandomQuote()
and delete this line of code:
purchaseBttn.isHidden = true
This will keep the subscriptions button visible after a successful purchase and allow your users to switch between subscriptions.
Now, locate purchaseSubscription(_:)
and move the entire buyProduct
closure to a new method called purchaseItemIndex(index: Int)
. Notice that the index parameter is never used. Change products[0]
on the first line to products[index]
. The new method should look like this:
PoohWisdomProducts.store.buyProduct(products[index]) { [weak self] success, productId in guard let self = self else { return } guard success else { let alertController = UIAlertController(title: "Failed to purchase product", message: "Check logs for details", preferredStyle: .alert) alertController.addAction(UIAlertAction(title: "OK", style: .default)) self.present(alertController, animated: true, completion: nil) return } self.displayRandomQuote() }
You can now use this new method to purchase the relevant subscription based on the user’s selection. You’ll select the subscription based on the value passed as products[index]
.
Back in purchaseSubscription(_:)
, you need to add the alert to let users select their preferred subscription option. Add the following below the guard
:
let alertController = UIAlertController( title: "Choose your subscription", message: "Which subscription option works best for you", preferredStyle: .alert) alertController.addAction( UIAlertAction(title: "Monthly", style: .default, handler: { action in self.purchaseItemIndex(index: 0)}) ) alertController.addAction( UIAlertAction(title: "Yearly", style: .default, handler: { action in self.purchaseItemIndex(index: 1)}) ) present(alertController, animated: true, completion: nil)
You just added an alert controller with two options: a monthly subscription and a yearly subscription. You’ll send a request to the app store to purchase the relevant subscription based on the user’s selection in this alert, identified by the index
.
Now, all that’s left is to add the new product ID to the products array so the user will actually have two subscription options to choose from.
Go to PoohWisdomProducts.swift and change the name of poohWisdomSub
to monthlySub
. Then, add another product ID right below it. Name it yearlySub
:
public static let monthlySub = "com.razeware.poohWisdom.monthlySub"
public static let yearlySub = "com.razeware.poohWisdom.yearlySub"
Make sure you replace the product IDs of both subscriptions with the ones you created on App Store Connect.
Change the initialization of the productIDs
array to include the two subscription products:
private static let productIDs: Set<ProductID> = [PoohWisdomProducts.monthlySub, PoohWisdomProducts.yearlySub]}
Now, productIDs
contains both available subscriptions and the app will request the relevant one from Apple’s server based on the user’s selection in the subscriptions alert.
Finally, go back to ViewController.swift and in viewDidLoad()
, replace the condition checking if the user has purchased poohWisdomSub
with one that checks either of the two available subscriptions. Your condition should now look like this:
if (PoohWisdomProducts.store.isProductPurchased(PoohWisdomProducts.monthlySub) || PoohWisdomProducts.store.isProductPurchased(PoohWisdomProducts.yearlySub)){ displayRandomQuote() } else { displayPurchaseQuotes() }
You’re now displaying a quote if the user has purchased either subscription.