In-App Purchase Tutorial: Getting Started
Learn how to grow app revenue in this in-app purchase tutorial by allowing users to purchase or unlock content or features. By Pietro Rea.
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: Getting Started
30 mins
- Getting Started
- Creating an App ID
- Checking Your Agreements
- Creating an App in iTunes Connect
- Creating In-App Purchase Products
- Creating a Sandbox User
- Project Configuration
- Listing In-App Purchases
- Purchased Items
- Making Purchases (Show Me The Money!)
- Making a Sandbox Purchase
- Restoring Purchases
- Payment Permissions
- Where To Go From Here?
Payment Permissions
Some devices and accounts may not permit an in-app purchase. This can happen, for example, if parental controls are set to disallow it. Apple requires this situation to be handled gracefully. Not doing so will likely result in an app rejection.
Open IAPHelper.swift again. In the StoreKit API extension, replace the return
statement in canMakePayments()
with this line:
return SKPaymentQueue.canMakePayments()
Product cells should behave differently depending on the value returned by canMakePayments()
. For example, if canMakePayments()
returns false
, then the Buy button should not be shown and the price should be replaced by “Not Available”.
To accomplish this, open ProductCell.swift and replace the entire implementation of the product
property’s didSet
handler with the following:
didSet {
guard let product = product else { return }
textLabel?.text = product.localizedTitle
if RazeFaceProducts.store.isProductPurchased(product.productIdentifier) {
accessoryType = .checkmark
accessoryView = nil
detailTextLabel?.text = ""
} else if IAPHelper.canMakePayments() {
ProductCell.priceFormatter.locale = product.priceLocale
detailTextLabel?.text = ProductCell.priceFormatter.string(from: product.price)
accessoryType = .none
accessoryView = self.newBuyButton()
} else {
detailTextLabel?.text = "Not available"
}
}
This implementation will display more appropriate information when payments cannot be made with the device. And there you have it — an app with in-app purchase!
Where To Go From Here?
You can download the completed version of the project using the Download Materials button at the top or bottom of this tutorial. Feel free to re-use the IAP helper class in your own projects!
The In-App Purchase Video Tutorial Series by Sam Davies covers all of the topics introduced here, but goes to the next level in Part 3 where he talks about validating receipts.
One shortcoming of the sample app is that it doesn’t indicate to the user when it is communicating with Apple. A possible improvement would be to display a spinner or HUD control at appropriate times. This UI enhancement, however, is beyond the scope of this tutorial. For more information on HUD controls, check out Section 3 of The iOS Apprentice.
Apple has a great landing page for in-app purchase: In-App Purchase for Developers. It collects together links to all the relevant documentation and WWDC videos.
IAPs can be an important part of your business model. Use them wisely and be sure to follow the guidelines about restoring purchases and failing gracefully, and you’ll be well on your way to success!
If you have any questions or comments about this in-app purchase tutorial, then please join the forum discussion below!