Join Ron Buencamino for an in-depth discussion in this video Making a product request, part of Implementing In-App Purchases in iOS 11 with StoreKit.
- [Instructor] When requesting an available products list from the App Store, we need to provide a list of product identifiers to query against. Anything that matches will be returned to us in our SK products request delegate call back. So I'm going to create a function called getProductIdentifiers that will return an array of strings, each being an identifier of a product that we'd like to offer. Now providing this list of identifiers can be done in two ways. We could bake our app with the product identifiers already entered or we could make a call to the web server that we host and retrieve a list of identifiers.
Now there are advantages to both. If you were to bake your app with product identifiers, you'd simply be hard coating these items into an array that you would include with your product request. Now while this is the easiest of methods, it's also the least scalable, as you would need to submit a new app update each time you wanted to add or remove products for sale. Now when working with the web server, you can dynamically update your list of products on it, and each time your store loads, it can make a call to your server and update the available products list.
This in conjunction with marking products as available in iTunes Connect, can provide a good way to keep your storefront feeling fresh without requiring users to update their app all the time. This lesson though will not go into how to put together a web server to do this as there are a number of ways you can go about this task. However, for our get product identifiers function, we're going to mimic a response from our web server by building an array of items based on our product.plist file.
So what I'm going to do first is create an instance variable called identifiers. We set identifiers to be an array of strings and we initialized it to be empty. Next, what we want to do is check our bundle for our products.plist file, and if we find it, I want to go ahead and populate our identifiers array with the contents of our products.plist So let me go ahead and do that right now.
So at line 31, I'm checking our bundle for a URL to our products.plist resource, and if it exists, on 32 I'm creating an array from it, and at line 34, I'm iterating through that array and taking each of the contents and appending it to our identifiers array, and at line 39, we're simply returning that at the end of our function. Next, what I want to do is create a worker function that will receive the identifiers and set up the actual product request. So I'm going to create a function called performProductRequestForIdentifiers, and it's going to receive an identifiers array argument.
So at line 43, what I'm doing is creating a set based off of the identifiers that's being passed into this function. On line 45, we are creating our SK product request. On line 46, we're setting the delegate to be the self, and on line 47, we're starting that request. Now, once that requests starts, once it receives a response, then it's going to call our delegate callback that we set up over here on line 51.
Next what we want to do is set this all off into action, so I'm going to come over here to AppDelegate, and inside of our application did finish launching with options function, I'm going to go ahead and perform our setup check. Okay, so what I did here in AppDelegate on line 19, is I called our setup purchases function which, if you remember, checks the device to see if we are in fact capable of performing any kinds of in app purchases.
If it returns as successful, then I'm calling our function perform product request for identifiers, and for the identifiers, I'm calling our get product identifiers function that created based off of the product's plist file. So let me go ahead and make sure my target is set to my device, and we're going to go ahead and run it and verify that we get a printout of our products. As we could see here in our console, when the callback occurs, I have products and we see that I was returned different SK products, so we verified that our SK product request went through and the delegate callback occurred, and we were able to update our products property.
So now that we're able to retrieve products from the App Store, let's move on to our next lesson where we're going to present these items in our storefront.
First, get an overview of StoreKit and the types of purchases you can offer, including consumable and non-consumable products and non-renewing and auto-renewable subscriptions. Next, learn how to configure new products for sale with iTunes Connect and display a storefront right within your app. Ron then shows how to request and process payments, and deliver users' purchases to their iOS devices. Plus, learn about restoring purchased content—a smart strategy for keeping your app rating high—and requesting reviews from users using the new SKStoreReviewController API, which doesn't require leaving the app.
- What is StoreKit?
- Creating products in iTunes Connect
- Displaying products in your storefront
- Requesting payments
- Receipt validation
- Delivering products
- Restoring purchased content
- Asking for app reviews