How to add and test In-App Purchases to your iOS app!
Tags: swift, ios, tutorial
Published: 2020-05-25 19:31:22

In-App Purchases can end up making you tons of money with your next app…
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oz5zhhxzlg6t17jn3878.jpg)
Why In-App purchases?

If you haven’t noticed, the biggest apps in the market like Spotify and Evernote are free, which attract far more users than any paid app. What makes these apps different is that they have In-App purchases which usually far surpass any income a solo developer can accumulate with a paid app. If your incentive is to not only help others and create something that the people want, but also making a good amount of money off of it, In-App purchases are perfect for you. Here is a short, easy tutorial on how to implement In-App purchases into your next app in just 13 steps…

1) Go to App Store Connect

First, go to App Store Connect (https://appstoreconnect.apple.com/)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q0r4g795fi2w0m4y495b.png)

2) Check the Paid Apps Status

Press the Agreements, Tax and Banking button to check if you’re able to create an app with any paid components in it. If it says Active, then your fine. If the dot under status for the Paid Apps type is red, then you have to add certain information before creating this app.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l9txo03oong8epcbq2o4.png)

3) Registering the App ID

Now we need to register the App ID (https://developer.apple.com/account/resources/identifiers/bundleId/add/) .

First, for the description, write something quite generic or just the app name. The Bundle ID will be set to explicit. The Bundle ID has to be unique, so the convention usually is the following:

    If you have a website, then write com.[websitename].[appname]
    If you don't have a website, then write com.[fullname].[appname]

Although the capabilities section is preset, its good practice to scroll down in the Capabilities section to check that In-App Purchases is enabled.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/amo59yv3rspnh5uwujg7.png)

4) Setting the Project Info

Next, open up Xcode and create a project with the same App ID description. This is a requirement because, as you can see, the Bundle Identifier is heavily dependent on both the Product Name and the Organization identifier, which follows nearly the same form:

    For those with a website, it will be com.[websitename]
    For those without a website, it will be com.[fullname]

For this project, we will be using Swift and the Storyboard User Interface.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tkg0a9fdi5f8x0yr1sk7.png)

5) Adding the In-App Purchase Capability to Xcode

After successfully creating the project, go to the project file and then to the Signing and Capabilities section. For the latest version of Xcode, you will have to go to the top left corner of this section which says “+ Capability” to add a capability. After the window opens, search for the In-App Purchase capability and add it to your project.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/58qxyrrgn5n388onayki.png)

6) Creating the New App

Now, let’s go back to App Store Connect. On the upper left corner, there will be a “+” sign, where you will have to press the New App button in order to create a new app.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/y3nrsgze20xp2fa9cmib.png)

7) Setting the New App Info

Upon pressing the button, the following window will pop up. Here we will select the platform, which for us, will be iOS. First, start out by entering the name of the app. Remember, the name has to be unique, and while the description was quite clear, it isn’t counted and compared to the other existing apps.

In our case, I named the app InAppPurchase 1023. The primary language is quite self-explanatory and the Bundle ID will be the same bundleID we created before for this app. The SKU is basically a unique string that won’t be displayed anywhere. I encourage following this format for the SKU:

[2 or 3 letters][00]

The 00 will then change to 01 an on as you create more apps. The user will have access to the full app, so be sure to select Full Access.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jkzd6qqnsthlbe67a5nu.png)

8) In-App Purchase Types

Now, let’s go to Features and then In-App Purchases. When adding an In-App Purchase, make sure to only check Non-Consumable.

While Apple explains these 4 types of In-App purchases, here is a short, easy to understand the description of them:

Consumable: You can only use this once, and once you have to use it again, then you have buy it again. This applies to games that use coins, etc.

Non-Consumable: As soon as you buy a non-consumable In-App Purchase, it will stay with you forever. This usually applies to unlocking special levels in games, premium versions of apps, etc.

Auto-Renewable Subscriptions: These are great for apps like Spotify, etc. As soon as the subscription is bought, it will keep on renewing after the desired amount of time automatically unless the user chooses to cancel the subscription.

Non-Renewing Subscription: As soon as the user buys the subscription to the app, it will not automatically renew, but the user has to renew it manually.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gq10xfkbbvyvwvmcsb77.png)

9) Setting the price

After pressing Create, the box below might take some time to fully process. Upon finishing, you will see a drop-down which will show the different Tiers of prices. We will go with Tier 1, so if the sandbox section of this tutorial does not work out for you, it's only going to cost you 99 cents. To save some times, copy the Product ID as will be using that later.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bn4c1jawcjrgx9k87r8k.png)

10) Setting up the UI

Now, let's go back to Xcode and into the Main.storyboard file. By using Cmd+Shift+L, we can search and add a UIButton to our interface.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h2fb3hryjtpykqtmr7pm.png)

Lets set the button horizontally and vertically in the container.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kg71bujogehy1pguhif2.png)

Set the width 150 and the height to 50.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pujbx1srwsx0y3blkj3r.png)

After that, once again, use Cmd+Shift+L to create a Label and set its alignment constraints to be horizontally in the container.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ia5nohic60ihedc3enwj.png)

Now, let's go to the ViewController.swift file, where will paste the ID we had copied before into a new constant called PurchaseID as a string.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uu5h236peuh857mer1k4.png)

To make it a little bit more neat, lets set a couple more additional constraints in terms of the y position.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r6p6xj7yi7ka8ioze7va.png)

Now, by holding Control and moving the label to the View Controller. Set the type to UILabel and the name to buyLabel.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o7xo1ppevjaokj9t5jpb.png)

Let's use the same concept for the button and name it as buyPressed.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sm0cks02qi69yemh3e64.png)

First, import the StoreKit in order to access certain commands for the In-App Purchases ability. After the colon and then the UIVewController, use the SKPaymentTransactionObeserver.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/exk2mbrnubwzy27t7pw2.png)

11) Let’s start the actual programming!

Create a function called as payment queue to handle the transaction states. Now in the function viewDidLoad() we will add the SKPaymentQueue, which as described by Apple, is a “A queue of payment transactions to be processed by the App Store.”. If you aren’t that proficient with Swift, I really recommend to look at this link for further information: https://developer.apple.com/documentation/storekit/skpaymentqueue
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/82727eyfqr4csqubs5ki.png)

In the paymentQueue function, we will add the true content of this tutorial into it. Use the if-else statement to see if the transaction state is purchased, and if it is, let the console print back “Transaction is successful” just to be sure and change the label of the buyLabel to the same. Now use else if to see if the transaction failed or not and change the label to “The transaction has failed.”
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sakgvurfgbjx4t95dci7.png)

According to Apple, the SKMutablePayment class is “A mutable request to the App Store to process payment for additional functionality offered by your app.” We will name the constant accordingly, as transactionRequest. Within this, we will “tell Apple to recognize” the purchaseID as the productIdentifier and “add” the same transactionRequest to the SKPaymentQueue.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kmo7ksj7d62usjyzwjl7.png)

12) Setting up testing

Now, its time to test our app! Instead of paying to make sure it works, we have to create a “sandbox account”. To do this, let's create a fake account on google. This will be your “sandbox account” from now on. You can’t use any of your own accounts since they will be associated with an Apple ID (or have been).

After entering the data below, Google will ask for your birth date, the country you live in, and phone number. You need to type in the right phone number for this to work as you will need to verify the account. Also, do NOT add a backup email, since it will be associated with your Apple ID (in most cases).
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cafvzwrvmf5gqak4baal.png)

Now, let's go back into App Store Connect and into Users and Access. To the bottom left, there will be a section in bold called “Sandbox” as shown by the image below. Press on Testers under Sandbox. Here, you will notice a blue plus icon which will allow you to create a new tester. Here, enter the required information.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/y1wepr8jv4bjg8y5zugp.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7k6l824dmd5v9n78g4gx.png)

13) Testing

Done!

Let's go an run your app on your device, NOT the simulator, as this won’t work if you use a simulator. First, to avoid any chances of paying (although 99 cents won’t hurt) go to Settings. In Settings, scroll down to iTunes & App Stores. Under here, you should see a “Sandbox Account” section where there should be your tester account. If it’s there, then congratulations, you did it! If it isn’t, go back to the beginning and try again.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lcgcx01un7h1kwqmmc57.jpeg)

Thank you for reading my tutorial! It means a lot, and I’m sure that others would want to see this too, so don’t forget to 👏 and follow me on Medium at: https://medium.com/@suryanshmansha