In-App Purchase How To – Part 1

A reader of the “theapplady” blog asked me to create a tutorial that explain the steps to implement In-App Purchase in an iOS application’s tableView rows. I responded sure, but it will take a while to whip up a tutorial and present it to you. The reason for this is because the process of implementing In-App Purchase in an iOS application is a long and complicated process; especially for a newbie.

Code presented in this tutorial series assume you are using Xcode 7.3 and Swift version 2.2. So if you are using a older or newer version of Swift, it may produce errors. Fix them by using Xcode’s Fix-it tool. Also, I assume you aren’t a newbie to the Swift Programming Language and you know your way around Xcode Integrated Development Editor.

In this tutorial series I walk you through the entire process of implementing In-App Purchase in an iOS application I created just for this tutorial. There are 6 parts to the tutorial and you are reading part 1.

What is In-App Purchase?

In-App Purchase (IAP for short) is a feature you implement in an iOS or Mac application that enable the app user to unlock built-in app functionality or gain access to some form of premium content. Here are the most important things you should know about IAP.

IAP Does provide the you, the app developer the ability to

  • provide product information in your iOS/Mac app
  • restore previously purchased products in your iOS/Mac app

IAP Does not

  • download the product the user purchased via your app
  • unlock the built-in app function/content the user purchased via your app
  • update the app’s views, during and after the IAP process.

IAP only

  • process payment of the products the user purchased within your iOS/Mac app
  • return a completed purchased transaction to your iOS/Mac app

In-App Purchase Product Types

Now, there are five types of products an app user can purchase in an iOS/Mac application. The table below list, describe, and provide an example of the five product types.

Type Description Example
Consumable A product of this type can be purchase multiple times. Your app must unlock once the use purchase it. Once this product is depleted, the user will have to purchase it again. This product type cannot be restored on devices associated with the user’s Apple ID. coins in a game
Non-Consumable A product of this type is purchased only once and it never expires. This product is built into your app or hosted on Apple’s servers. Your app must unlock it once the user purchase it. This product type can be restored for free on devices associated with the user’s Apple ID. audio or video file
Auto-Renewable Subscription A product of this type can be purchased multiple times. This product type is built into your app. Your app must unlock it once the user purchase it. This product type is automatically renewed unless the user opts out of the renewal. You specify when this product type expires. This product type can be restored for free on devices associated with the user’s Apple ID. recurring delivery of newspapers or magazine issues
Free Subscription A product of this type is purchased only once for free and it never expires. Free subscriptions never expire and can only be offered in apps that are in the Magazines & Newspapers category. After a user sign up for a free subscription, it is available on all devices associated with that user’s Apple ID.
Non-Renewing Subscription A product of this type can be purchased multiple times. This product type is built into your your server. Your app must unlock it once the user purchase it. Your app is responsible for:

  • calculating the time that the subscription is active
  • determining what content needs to to be made available to the user
  • detecting that a subscription is approaching its expiration date and prompting the user to renew the subscription by purchasing the product again.
  • making the subscription available across all the user’s devices after it is purchased and for letting the user restore pass purchases.

This product type is often used when your users already have an account on your server that you can use to identify them when restoring content.

monthly subscription to a database of audio or video streaming feed

Overview of The Tutorial’s iOS App

First of all you’ll use an Xcode project I created for this tutorial implement the ability for the user to perform five main tasks:

  1. View a list of four Non-Consumable products (music videos)
  2. Tap a Buy Now button to purchase a music video
  3. Play the purchased music video
  4. Restore all purchased products (music videos) by tapping a Restore button
  5. Automatically restore all purchased products before the user sees the ProductView

The name of the Xcode project is Nifty-IAP and it has two views as shown in Figure 1-1 below. As you can see by the arrow in Figure 1-1, the VideoView is shown only when the user tap a cell in the tableView cell’s accessoryView containing a checkmark. If a cell’s accessoryView doesn’t have a checkmark, nothing happens when the user tap the cell.

niftyiap-figure1-3
Figure 1-1

As you already know, the Restore button on the ProductView restores previously purchased Non-Consumable products (music videos). Figure 1-2 below shows what the ProductView will look like when the user tap the Restore button. Check mark appear only in the cell’s accessoryView of the product the user purchased.

Figure 9-3
Figure 1-2

Assumptions Made About You

Before I show you how to implement the Non-Consumable IAP type in this tutorial’s application, I assume the following about you:

  • You want to implement In-App Purchase by way of “Buy Now” buttons.
  • The app user won’t be purchasing many products via the iOS application.
  • All purchase products are built into the app itself. For example, the app user won’t be downloading purchased products.
  • You are enrolled as an individual in the Apple Developer Program; that way you can implement and test IAP in the tutorial’s iOS application.
  • You have a real device to test IAP and to restore purchases.
  • You have experience in using Xcode IDE and the Swift language.

Download The Nifty-IAP Project

Here’s the link to download the Nifty-IAP project you will develop through out the tutorial. After unzipping the file, move the Nifty-IAP folder to the Interactive Swift Programing folder.

download-excodeproj

The Single View Application template was used to setup the project with options shown in Figure 1-3 (b). Further more, when Xcode prompted me to save the project, I ticked the “Create Git repository on” checkbox.

niftyiap-figure1-0
Figure 1-3

You should launch the project in Xcode and take it for a spin on your real device to see how it work. As of now, you’ll only see the ProductView. The first screenshot in Figure 1-4 below shows what the ProductView look on your iPhone device. The second screenshot show result of tapping the view’s “Restore” button.

niftyiap-figure1-1
Figure 1-4

Pre In-App Purchase Tasks

There are two sets of pre In-App Purchase tasks you’ll do in this tutorial. As you can see in the cards below; you’ll do the first set in the Xcode project, you’ll do the second set in iTunes Connect web portal.

tasks-card1

tasks-card2

In-App Purchase Tasks

Once you’ve completed above pre In-App Purchase tasks, you will perform In-App Purchase tasks shown on this card.

tasks-card3

Restore Purchases Tasks

Once you completed above In-App Purchase tasks, you will perform Restore Purchases tasks shown on this card.

Card 4
Card 4

That’s pretty much it for today. Next week you get started in performing tasks in the first card shown above. In the mean time, keep above cards handy because you will use them to keep track your progress.