Everybody was going on this is Caleb with Debb slopes.
And in this video we're going to go ahead and add to our Constans File.
We're going to also create some enumerations to help us know what products we're talking about when
we are doing this whether it's a meal or the hied adds non consumable product.
And then we're also going to go ahead and set up another enumeration to help us determine the purchase
status whether we purchase something whether something failed or whether it was restored.
Then we're going to go ahead and write the function.
You've been waiting for make purchase for item with product index.
This function is going to basically make a request and it'll present the would you like to buy this
item and then I'm going to have to show you how to set up your testing sandbox account and yeah.
So let's go ahead and let's open up our project Let's get started right now.
So right click on food Zyla create a new group called gnomes and Roona right click again and create
a new file.
Ok make it a swift file and we're going to call this product.
OK.
And just for now we're going to just say ENM product and we'll leave it like that.
We're going to create another new file click swift file and this is going to be purchase status like
so purchase status.
And that's also going to be an enumeration.
So Type in purchase status although I'm getting a ton of extra T's here and use some curly braces to
go ahead and create that.
Next we're going to go into our Constans file and we need to set up some identifiers one for our next
and app purchase.
But I'm going to go ahead and I'll.
All right here and then I'll use that one when I create our hide ads non consumable purchase.
But let's just go ahead and say let IAP hide ads ID.
And that's going to be equal to a string com dot IAP course dot hide ads.
OK.
And you know what let's put a little comment here let's just say IAP identifiers.
And now below let's do it again.
This is going to be for notification
identifiers.
And let me explain myself here.
We're going to use these identifiers as the name for notification center when you when you make a notification
send or call you need to pass in a name for the notification.
And these names are going to help us differentiate when we send a notification for a successful purchase
for a successful restore or a fail.
So let's go ahead and let's create three properties.
One is going to be let IPs service.
Well not all in caps sorry.
IAP service purchase notification and we're going to just said that to be a string value of the same
name and I'm going to go ahead and just copy this.
Whoops.
Select all and copy it and then paste it three times change.
Purchased here to restore.
Same with here.
Purchase needs to be changed to restore.
Then we're going to do.
Failure.
And failure.
Make sure that you type at the same exact way.
All right so now we have three ways to identify the different notifications.
We're going to be using.
And we're going to use those in just a little bit.
But for now let's go ahead and let's think what we need to do for product.
Now this is an enumeration meaning it doesn't.
Meaning it doesn't have an explicit value or it can't hold an explicit value unless we tell it to.
And we're going to.
So go ahead and under product we're going to have it be of type int.
It's going to subclass it and we're going to set two cases one case being meal and another case being
hied adds.
So go ahead and type case Neal.
And that's going to be equal to zero.
OK so we're going to be able to pass in a value and then that's going to be able to we're going to use
this to pull out the proper index in our IAP service remember our set of string has one item.
So we're not going to use the second index yet but eventually we're going to need to pass in two.
And so one of them is going to have the index 0 and the other is going to have the index 1 and so we're
using this as a way to call those specific values.
Next call case.
Hide ads and set that to be equal to 1 when it's there will use it.
But we're not going to use it for now.
So there is our enumeration that will help us to differentiate which product we're talking about next.
Go ahead and head over to purchase status and this is what we're going to use basically just to help
us determine a certain type of purchase whether it was purchased restored or failed.
So go ahead and type case purchased case restored and case
fail failed I failed typing failed.
Ironic.
Anyway.
So we don't need to set an explicit type because we're just going to use these names as identifiers
when we pass one in.
We'll be able to basically switch through and do something based on which one was passed in.
You'll see how we use that later.
So now that we have these all set up we can go right one more very exciting function inside our IP service
and it's going to let us actually make a legitimate purchase.
So let's go ahead and let's set it up down here beneath.
Product request and we're going to let me make some more space here so you can see and I'm going to
zoom in a little bit make my font a tiny bit bigger.
There we go.
And this function is going to be called Make purchase for item with product index.
So go ahead and type phunk make purchase or you know what I'm going to I'm going to attempt to purchase
because that's a little better of language for this attempt to purchase for item with.
And the princes will say product index of type product because remember we can use the raw value of
the product which is either 0 or 1 as an index to pass in which is really neat.
So then inside this function we're going to basically need to create two properties one to hold a product
and another one to hold an instance of S-K payment.
Okay.
So go ahead and type let product
and that's going to be equal to our products array k but we need to pull out a specific item at a specific
index.
Thankfully we have that.
So right now we're going to pass in product index like so.
And since it's of our enumeration we need to pull out the raw value of type int k which is either 0
or 1 since we only have one item it's going to be zero.
And that will give us the exact product we're trying to buy next.
Let's go ahead and let's create a payment.
And basically what this is going to allow us to do is to pass the product in and it's kind of like we're
saying I want to buy this item and we're going to pass that into another function in just a moment.
So Type let payment equals Eskay payment.
And if you look you can pass in an S-K product well by darn it we have one let's pass in our product
here that we just created thanks to our products.
And now all we need to do is access something called the S-K payment queue and it's a queue of payment
transactions to be processed by the AppStore.
Super simple.
So go ahead and type S-K payment queue and we want to access the default payment queue.
It's an instance of payment queue.
And then we're going to go ahead and type dot and check it out.
We just type ad payment and we have a payment called S-K payment boom.
So when we attempt to purchase it's going to pass in a product pass and a payment and add it to the
S-K payment.
Q This is pretty cool.
We pass in the payment.
It does some stuff and then it actually is going to call a delegate on a special protocol called S-K
payment transaction observer.
Similarly to how product request is called as soon as this product request comes back from the App Store.
S.K. a function inside of S-K payment transaction observer is going to be called.
And then we can tell you know we can tell our app what we want it to do.
OK.
So that's awesome.
But we need to basically set it up so that we can utilize this S-K payment.
Q And to do that we're going to go ahead and extend our IAP service so that we can conform to S.K. payment
transaction observer.
So let's make some more space here.
An online 58 I'm going to say extension IAP service and we're going to conform to S.K. payment transaction
observer.
Okay.
Now it says a set of methods that process transactions unlock purchased functionality and continue promoted
in-app purchases.
Press Enter give it some curly brackets and the function we're looking for here is called payment.
Q updated transactions.
Okay.
So this is going to be called any time we purchase something.
Time we have a failed transaction we restore a transaction.
Payment is deferred or we're in the process of purchasing.
We can do we can call a function on any of these.
So press enter and we're going to dive into Well first we need to see if the error goes away if we've
properly conformed.
Looks like we have and what we need to do is we need to set up a switch to basically switch through
all of those different things.
I just said purchased failed restored deferred or purchasing.
And it's really easy.
So all we're going to do is we're going to look through all the transactions we have passed into our
S-K payment queue which get returned here.
Okay.
We're going to cycle through all of them with a for loop and for each one we're going to switch through
all of the potential cases and then for the matching case we can call a certain property or a function.
So let's go ahead and call for transaction in transactions.
We're going to write a switch and we're going to switch through a property in transactions so-called
transaction transaction state and you'll see that S-K payment transaction state is actually an enumeration.
And so what we can do is we can switch through all the potential cases that there are.
So let's do the first one case dot purchased.
That's the most common is that it will be successfully purchased.
And for now we're just going to call break then we're going to call case let's do restored next and
we'll just break next case failed.
And break.
Next case is deferred if payment is deferred then we'll call case and purchasing which is called when
like you start the purchasing process OK which can be helpful for certain things.
So now if I go ahead and build this it's going to say that we have no heirs.
We have exhaustively gone through every potential case.
There are only five cases in this transaction state so we're good.
So basically what's going to happen is every time that we purchase and we are successful in purchasing
it's going to call this here case that purchase.
So for now I'm just going to use Debug print and I'm going to print out purchase was successful.
All right.
So that looks pretty good.
And for now I'm just going to leave it at that.
We'll we'll come back later and we'll set up some stuff for the purchase to happen.
But you know what we need to actually tell it that our purchase is finished because right now it's returning
and it's saying hey looks like it was good but we need to basically use our S-K payment cue to let it
know that it's finished and that it can release that that transaction.
So inside of case up purchased we're going to call S-K payment.
Q Okay.
And we're going to call that default to get the default payment.
Q Then we're going to call a finished transaction and the transaction will just come from our for loop.
So go ahead and type transaction and now the payment is officially finished it's notified.
It's good to go.
We can print purchase was successful and then we can break.
Beautiful.
I want to try it out.
So in order to actually try attempt to purchase for item we need to go into our detail Visi because
that's where our buy button is that's where we want to actually try this.
Right.
So we need to find a way to call our function and we can use our IP service.
So in buy button was pressed.
All we need to do is type IAP service instance WIPs instance dot attempt to purchase for item with product
index and since we're using our enumeration we can just type dot meal because that's the index of the
item we want to attempt to purchase.
I think we should try this but at this point we're going to have to build our project to a physical
device in-app purchases work very inconsistently slash not really at all on simulators and so we're
going to have to go ahead and build it to a physical device so you're going to have to plug one in over
us beat in order to test it and let's do that now I'm going to build it to my phone.
We're going to pull up in quick time here so that I can actually show my iPhone screen up on the screen
here for you to see although I'm going to do one of the time because quick time likes to hijack the
USP port not audio recording movie recording ok cool.
There we go.
So we're good to go there.
That looks awesome.
And let's go ahead and build and run it again.
And then I need to show you how to set up our test account.
OK.
We're going to need.
I didn't connect so that we can remember the e-mail for our sandbox user.
And you know what it looks like our app has built successfully looking pretty good here.
So go to users unrolls go to sandbox users as soon as it loads which can take a while.
If your internet is slow 9:58 you do OK.
Click on sandbox Tester's and the name we made was Mr. test one two three at gmail.
So let's use that.
All right.
So check it out guys.
What we're going to do.
We're going to attempt to make a purchase.
You can see the IAP products have loaded successfully.
Let's go ahead and pull open salmon and let's try to buy this item for nine ninety nine when I tap the
button.
It's going to attempt a purchase.
And let's go look at that function against we remember what it does.
Passes in the product makes a payment with that product and adds it to the payment queue when the payment
queue is good to go.
If the purchase is successful it's going to finish the transaction and print purchase was successful.
So let's go ahead and let's tap on that button and you don't need to show you my screen here don't I.
I tapped on it and it says use existing Apple ID.
OK.
So we created an account and I S T our dot test 1 2 3 at gmail dot com GM.
That's a capital it's hard to type with one hand.
Gmail dot com our password was capital-T test test one two three exclamation mark.
Now when I push by we need to see what what's going to happen here we pushed by and it's telling me
I need to verify my Apple ID for in-app purchases.
It's kind of annoying.
You have to log in twice.
Isn't that fun.
So now I'm going to type Mr. dot test and see if I can do this a little faster this time.
One two three at gmail dot com.
And my password one two three exclamation mark so press OK and let's see what happens.
We're waiting we're waiting.
Nothing's happening yet.
My internet is being very slow today.
So you know what it looks like it might actually we might need to log into that account before we can
actually make a purchase.
So now that I'm logged in let's go and try it again by item.
OK.
So this is good this is what it should look like.
You should have to enter your password.
I guess the first time was just initializing the account.
And now if I type in my password and I push OK watch what happens.
We should get a pop up confirm your purchase and it's going to tell me to verify don't do it because
once you verify it in iTunes then it locks it in as like a legit Apple ID.
So don't do that but it says Do you want to buy one meal for 999 that's what we typed.
We named meal.
That's exactly right.
So watch what happens when I push by.
Is he going to do anything.
Is it going to work is it going to work.
Your purchase was successful.
That is seriously the coolest thing.
Oh my word.
We printed what we didn't print.
Interesting.
So I wonder why it didn't print.
We successfully paid
and that's good but it appears that we did not successfully print.
That's interesting.
Let's try it again let's try to buy it because it's a consumable we should just be able to buy it over
and over again oh interesting so it looks like it is showing up as a non consumable purchase.
And it's not successfully making it to the purchased to the purchased payment.
Q So let's figure out what the problem is here.
Shoot what's going on oh you know what I realized what the problem is you know what.
Let's see.
Yeah.
So in order for any of this to work we need to set it up so that when this view controller or sorry
when this class is initialized we need to set the payment queue.
We need to add this class specifically to the payment queue.
So go ahead and type in net and we're going to override the initialiser and call super in it like so.
And then what we're going to do is call S-K payment.
Q default.
And we need to add an observer.
This class needs to be observing all of the payments because otherwise we can't.
And you'll notice it's asking for an observer of type S-K payment transaction observer.
We've already conform to that protocol here so we can justifiably call self.
That was the problem.
So let's go ahead and let's reload let's let's build and run and we'll we'll check out to see if that
fixed our issue.
Here we go.
And you know what we are not on the sim we are in quick time here.
All right.
Very cool.
Here we go.
Pull open this tap by item tapped by one meal your purchase was successful.
Look it's printing purchase was successful.
That's exactly right we can buy it over and over again.
Your purchase was successful.
Now it's able to recognize that this is a consumable purchase and every time we purchase it's going
to print out purchase was successful.
Guys we are successfully implementing consumable in-app purchases.
That's all it takes based on this.
You can use a variety of methods to tell other controllers that the purchase is successful like delegates
or notifications or whatever.
But guys the essence of this course for consumable purchases is basically finished.
We have set up a consumable in-app purchases.
We're going to keep making this better.
Don't you worry.
But for now this is a really really good start guys so we can now make consumable purchases but we need
to start thinking about how we're going to notify controllers that we have successfully made purchases.
How are we going to hide the ads for instance what are we going to do to hide the ads.
How is it going to know that we have successfully made a purchase so we're going to do that in the next
video.
So let's get over there and let's start that now and really excited guys.
This is looking so good.
Awesome work.
Let's head over there now.