Here, you will find the step-by-step process of implementing IAP with Swift
When it comes to iOS applications, we have a lot of choices to choose for monetizing our iOS apps. Some of the well-known methods for making a decent amount of money from your iOS app are:
The in-app purchases also known as IAP allows the developers to charge the user a particular amount for accessing a specific content or functionality as an extra feature while using the application.
Here are some examples of in-app purchases:
There are four types of In-App Purchases (IAP) which are described in the following:
This is the type of IAP where the user will need to buy preferred items every single time if they want to use them. So, basically, you can not use these items for free in the future without paying for them.
The user may also lose the access to these items if the app is reinstalled or their smartphone is changed. Some examples of the consumable type are purchasing hints and collecting game currencies.
In auto-renewal subscription type of in-app purchases, the user can make a purchase of an item for a limited period of time. And once that limited time period is over then the subscription for that item will be renewed automatically.
Few examples of the auto-renewing subscriptions type are magazine subscriptions like Vogue and on-going services like Netflix etc.
In the non-consumable type of IAP, the user has to only pay for an item once and then that item is free to be used in the future. Even if the app is reinstalled or the device has been changed, the item will still be accessible and will not lose its data.
Few examples that fall into the category of non-consumable IAP type are a PRO version app upgrade, removal of advertisements from the app.
The last type of IAP is the non-renewing subscription in which the end-user is able to use the item for a fixed period of time and after that time period is passed that item needs to be purchased again for using it.
The best example of a non-renewing subscription is a membership period or a sports session pass for 6 months access.
iTunes Connect Setup
In order to set up your iTunes connect, few things are to be done prior like:
Always keep your code in a separate class, for example, TIAPHandler is the name of the class. In this class that you created, store all the information related to the In-App Purchases.
Note: The property names 'isLogEnabled' will help you to console the log of the class TIAPHandler. And the 'shared' property will let you interact with every member function that you need to use during the IAP services.
Before we proceed, you need to create few variables in Swift for your IAP product related to the type of IAP that you selected earlier. Creating variables at this point is important because these variables will be handling you IAP request and also an array which will store all your available IAP products.
Now that you have selected the Class Function, it's time to set the Delegate method in order to make your product return back using Swift.
First, open the UIViewController and the create an empty array of the type SKProduct using Swift. Then in the viewDidLoad function, set your IAP product IDs and also fetch all details regarding those IAP products.
Note: The user also has the choice to display these details in UICollectionView or UITableView.
TIAPHandler.shared.fetchAvailableProducts()
TIAPHandler.shared.purchaseStatusBlock = {[weak self] (type) in
guard let strongSelf = self else{ return }
if type == .purchased {
let alertView = UIAlertController(title: "", message: type.message(), preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: { (alert) in
})
alertView.addAction(action)
strongSelf.present(alertView, animated: true, completion: nil)
}
}
After that create an action where you will initialize the transaction window of the IAP services. Like this -
@IBAction func consumable(btn: UIButton){
TIAPHandler.shared.purchaseMyProduct(index: 0)
}
To test your code, first sign in with your Sandbox user from your iOS device. Then go to your iOS application and select the action which will initiate the transaction. Because you are using a sandbox user, you won't be charged for the transactions that you make for testing out your IAP services.
Now that we have completed the steps of implementing IAP in Swift, we should also know the reasons why IAP services are important and should be used for your iOS applications:
We hope that this article will provide you some great insights regarding the In-App Purchases and its services. Don’t forget to check out our articles on Swift as well.
Aparna is a growth specialist with handsful knowledge in business development. She values marketing as key a driver for sales, keeping up with the latest in the Mobile App industry. Her getting things done attitude makes her a magnet for the trickiest of tasks. In free times, which are few and far between, you can catch up with her at a game of Fussball.
Cut to the chase content that’s credible, insightful & actionable.
Get the latest mashup of the App Industry Exclusively Inboxed