Preparing an iOS 11 Application for In-App Purchases


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book


The previous chapter provided an overview of the mechanism by which in-app purchasing can be implemented within iOS applications. The next few chapters will take this knowledge and put it into practice by working through the creation of an example application containing locked content that can only be accessed by the user after making an in-app purchase.

An important part of supporting in-app purchases occurs before any code is written and is the topic of this chapter. These steps involve the creation of an App ID with in-app purchases enabled, configuration of code signing within Xcode and the use of iTunes Connect to create a test user, application and in-app purchase item. Once these steps have been outlined, the next chapter, entitled An iOS 11 In-App Purchase Tutorial, will focus on the application development process.

About the Example Application

The example application created in this chapter will simulate the requirements of a fictitious, multi-level game whereby the user is provided with access to level 1 when the game is first installed, but must make an in-app purchase to unlock access to a second level.

For ease of testing, the in-app purchase must be performed each time the application runs. In a real world situation, however, a purchase of this type would be considered to be a non-consumable purchase and the application would need to check at startup whether the purchase had been made and unlock the premium level appropriately.

Creating the Xcode Project

Launch Xcode and create a new project based on the Single View Application template. Enter InAppDemo as the product name and set the Language menu to Swift.


Registering and Enabling the App ID for In App Purchasing

In order to use in-app purchases, an application must be associated with an explicit App ID rather than a wildcard ID. For example, com.ebookfrenzy.MyApp is a valid App ID for in-app purchase, while com.ebookfrenzy.* is not. In addition, the App ID must have In-App Purchase support enabled.

To register the application’s explicit App ID with the iOS provisioning portal and enable In-App Purchasing for that ID, select the application name target from the top of the project navigator panel. From the resulting project settings panel, select the Capabilities tab and locate and switch on In-App Purchase support as outlined in Figure 113-1, selecting a Development Team to use for the provisioning profile if prompted to do so:


Ios 11 in app demo enable iap.png

Figure 113-1

Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book

Configuring the Application in iTunes Connect

Enrollment in the Apple Developer program automatically results in the creation of an iTunes Connect account using the same login credentials. iTunes Connect is a web portal where developers enter tax and payment information, input details about applications and track the status of those applications in terms of sales and revenues.

Access iTunes Connect by navigating to http://itunesconnect.apple.com in a web browser and entering your Apple Developer program login and password details.

First time users should click on the Agreements, Tax, and Banking link and work through the various tasks to accept Apple’s terms and conditions and to input appropriate tax and banking information for the receipt of sales revenue. Failure to complete these steps in advance may prevent in-app purchases from working.

In order to test in-app purchases using the sandbox feature, it is first necessary to create a test user account. These are the account details that will be entered on the device when testing in-app purchases. Within iTunes Connect, click on the Users and Roles option. On the Users and Roles screen, select the Sandbox Testers category then click on the blue + button to add a new test user. When prompted, enter the credentials for the test user, taking care not to try to use an email address already associated with an active iTunes account.

Once the administrative tasks are complete, select the My Apps option followed by the blue + button to add a new iOS app. Provide an App Name (this must be a name that has not been used by another developer so may involve some trial and error) and a SKU Number (which can be any sequence of characters involving letters, numbers, hyphens, underscores and periods). From the Bundle ID menu, select the InAppDemo Xcode iOS App ID bundle created previously. Once the information has been entered, click on the Create button to add the app to the portal.

Creating an In-App Purchase Item

With the application configured, the next step is to add an in-app purchase item to it. Within iTunes Connect, select the newly created app from the My Apps screen and click on the Features tab option. In the resulting screen, select the + button next to In-App Purchases to add a new in-app purchase item. From the list of in-app purchase types, select the Non-consumable option to move to the In-App Summary screen where the following information will be required:

  • Reference Name – The name by which the item will be listed in iTunes Connect and in sales reports.
  • Product ID – The unique product ID for the item. This is usually constructed using the bundle identifier of the application with which the purchase is associated and must be unique. Make a note of the product ID you enter as it will be needed later in the example.
  • Price and Availability – The price of the item and whether the item is available for sale. For this tutorial, set the Price Tier to 1 and Cleared for Sale to true.
  • Display Name – The name of the in-app purchase item as it is to appear in the app store.
  • Description – For each language the application is required to support, enter a description for the item. Whatever you enter here will appear in the application later when the user is prompted to make an in-app purchase.
  • Content Hosting – Indicates whether or not content will be hosted on Apple’s servers for this purchase. For this example no content will be stored.

Once the settings have been configured, click on the Save button. Upon returning to the in-app purchases list, the new purchase item should be listed and we are ready to start developing the application in the next chapter.

Summary

Before any code is written for the application, a number of important steps must first be performed in order to support in-app purchases. This includes creating an appropriately configured App ID for the application. The iTunes Connect portal is then used to create a test account for testing of purchases, an entry in the App Store for the application and to declare and create items for the user to purchase. All of these steps are important and, if not completed accurately, can lead to problems occurring in the purchasing process.


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book