Join Ron Buencamino for an in-depth discussion in this video Create a SKPayment, part of Implementing In-App Purchases in iOS 10 with Swift 3.
- [Instructor] We last left off with being able to see our product list; however, whenever we tapped on our product, nothing happened. So in this lesson, we're going to discuss handling that interaction and making a payment request. So I want to come over to my IAPManager.Swift and I want to jump all the way down to the bottom of the class, and I'm going to create a new function. That function, we're going to call it createPaymentRequestForProduct. So the purpose of this function is to be able to take in a product and create a SKMutable payment out of it.
And then once we do that, we're going to add it to the default payment queue of our system so that we can submit it over to the App Store. So let's go ahead and do that, and then we'll explain the code as we get to it. Alright, let's take a look at this code that I put in here. As you can see, this is the bare minimum that you need to create a payment request for the App Store. You create a SKMutable payment and you need to pass it a product and we're using the product that is coming in when this function is called.
We're setting the payment quantity to one, and you have to have a quantity in your payment request so that it knows how many items to charge against. The quantity that we're choosing is one because our subscription item only should have a quantity of one. Our non-consumable item in the case of our photo note, we want it as a quantity of one. And our test items, we want those as a quantity of one as well, just so that we can continue to consume them. Now if we wanted to dynamically create this quantity, you can just pass in an argument that says the number that you want, and you can add it to the payment request, but you have to have a quantity with your SKMutable payment.
And so the last thing that we're doing is we're grabbing the default SKPaymentQueue, and we're adding the payment to it. Now when you do this, this kicks off the transaction process. It asks the user to login to iTunes if they haven't logged in recently, and then it'll ask them if they want to confirm their purchase, and if a user presses Buy, then it kicks off the entire transaction process with the App Store. So now let's work on calling this function that we just created. So I want to jump over to StoreTableViewController, and I want to go into the function tableView_didSelectRowAt indexPath.
What I want to do in here is grab the product that is in our products array at the indexPath.row value, and then I want to pass that product to the function that we're going to do to create our payment request. So let's go ahead and do that. So let's go ahead and run our app and see what happens. Okay, so I'm in our app, and I'm going to press the gear icon. And we see we have our Add On Store so I'm going to go ahead and tap on that, and then I'm going to tap on the StoreKit Test Item.
When I do, it asks if I want to sign in to the App Store, so I'm going to go ahead and do that. And I'm using a Sandbox account that I've created. Now when I press OK, it's going to log in, and then it's going to ask if I want to confirm my In-App Purchase, and it's asking if I want to buy my one item of StoreKit Test Item. And it's letting me know that I'm in the environment Sandbox. Now if I press Buy, again we're going to kick off the entire transaction process with the App Store. And if I press cancel, we just cancelled our purchase.
Now for us to be able to handle the entire App Store transaction process, we need to observe that default SKPaymentQueue. So let's go ahead and press Cancel on our window right now so we don't initiate a buy, and let's move onto our next lesson where we're going to talk about observing the SKPayment default queue.
First, learn what you can legitimately offer for sale in the App Store. Apple's guidelines restrict the sale of certain products, including real-world goods. Next, learn how to configure new products for sale with iTunes Connect and retrieve the product information in your app. Ron then shows how to request and process payments, and deliver the user's purchases to their iOS device. Plus, learn about selling subscriptions and restoring purchased content—a smart strategy for keeping your app rating high.
- What you can and can't sell in the App Store
- Looking at StoreKit
- Creating products in iTunes Connect
- Presenting products in your storefront
- Requesting payments in your app
- Processing transactions
- Delivering products
- Persisting purchases and auto-renewed subscriptions
- Restoring purchased content