With in-app purchases, nearly everything is tied to a product ID (or product identifier). As it turns out, there’s no way to fetch product IDs from the App Store, so you’ll need another way to load the product IDs into your Xcode project. For example, you could:
The first two options are typical implementations for scenarios where you have limited products or product updates. The third option is typically used when you have frequent updates and/or delivered content. For the gloopdrop project, you’ll use a custom struct.
Open the StoreProducts.swift file. This file contains the StoreProducts struct and the StoreProducts.Product class—both of which are custom classes. It also includes a large comment block at the top of the file.
Take a few minutes to read the comment block; there’s a lot of information in there, so take your time. When you’re ready, look for the following block of code:
| static let prefixID = "{your bundle id}" // include trailing dot |
| |
| static let productIDsConsumables: Set<String> = [ |
| "(prefixID){productid.qty}" |
| ] |
| static let productIDsNonConsumables: Set<String> = [ |
| "(prefixID){productid}" |
| ] |
Here, you need to update the value of the prefixID property to match your Bundle ID. You also need to add the product IDs you set up in Adding In-App Purchases, minus their prefixes.
For example, after updating the code, you’ll have something like this:
| static let prefixID = "net.justwritecode.gloopdrop." // include trailing dot |
| |
| static let productIDsConsumables: Set<String> = [ |
| "(prefixID)continue.1" |
| ] |
| static let productIDsNonConsumables: Set<String> = [ |
| "(prefixID)removeads" |
| ] |
Notice there are two static properties: one to hold the consumable product IDs and one to hold the non-consumable product IDs. Because these two properties are sets, you can add additional product IDs when needed.
For example, if you were to add more than one consumable, like a Continue (1x) and a Continue (3x), you’d code it like this:
| static let productIDsConsumables: Set<String> = [ |
| "(prefixID)continue.1", "(prefixID)continue.3" |
| ] |
Now that you have the product IDs set up in Xcode, the next step is to prepare the GameData class for storing purchases.