Hey what's up everyone.
Mark Price here at DEF slopes dot com and let's go ahead and purchase some product.
OK.
So what we're going to do is here in our purchased MANAGER We're going to create a function that our
view controller can call to actually start or initiate a purchase process.
So let's create a function here called purchase remove ads.
All right.
And what we're going to do is make sure first that a user can actually make payment.
So if Eskay payment queue that can make payments OK.
So to keep in mind is there maybe multiple reasons why someone cannot make a payment.
Maybe they haven't updated their credit card information.
Maybe there's something wrong with their account maybe it's a child on a phone that's had a parent lock
on it.
There's multiple reasons so you always need to make sure that a payment can be made before you actually
make the payment it will save time and headache because it goes back to server you won't get a failure.
So just make sure that they can make the payment first and let's make sure that products that count
is greater than zero.
So you can make a purchase unless you have the products that you received when you fetched him.
Remember you can't make a purchase unless you've already fetched them.
So we have to make sure we have one there just to be extra safe.
All right.
So let's grab the product that we want so remove ads product equals products zero.
OK so we're grabbing the first item out of the products array.
And again there's only one item here but you do receive your products in a particular order.
So when you receive products as they're listed in iTunes so they are sent down to you here in your app
so you can always be sure that the index and the rate that you're grabbing from is relative to what
is on iTunes.
OK.
OK so let payment equals Eskay payment.
What we're going to do is we're going to create a payment now and we're going to use the Move ads product
so the product just has meta data about your product but a payment is what's actually going to process
your payment.
OK.
And then what we're going to do is S.K. payment.
Q default.
AD So we're going to add an observer ourself as an observer.
OK.
And what we're going to do is as payment scale payment queue default add and we're going to add a payment
now.
So two different add phunk even though it's the same ad we're adding different things here.
So the first thing we're doing is adding this the this class purchased manager as an observer is going
to listen okay and then what's going to happen is we're actually going to add the payment to start that
request.
And the moment that you add this year it's going to start processing that payment request.
OK you with me so far.
So it's going up to the magical server and doing some magical things and then it's going to come back
to your app and give you a response.
Okay.
And that we do have a way to handle that and that's here in the updated transactions function that we
implemented earlier which is part of the S-K payment transaction observer protocol.
Okay let's go ahead and just get this going for each transaction in Tran's actions.
Okay.
So when the payment goes through there's one or more transactions that happened so you know you can
make multiple payments at the same time and it will have one or more transactions in there.
Okay.
And so what we're going to do is iterate through all the transactions have actively been actively been
going through when the callback is called here and then we're going to do something with it.
So let's go ahead and do a switch statement transaction trends action state.
So let's see what the state of the transaction is and what we're going to do is say if the case is purchased.
So if the item was successfully purchased what do we want to do.
All right and let's get the other let's get the other cases handled here as well too.
We're going to break if the case is not felt.
What do we want to do.
Okay.
And if the case is restored like when someone's restoring an EP purchase what do we want to do.
And then let's just put in a default here case default or not a case just the word default and we're
just going to break for now.
OK.
Restored with a D.
That's why that's yelling at us here.
OK.
So it's handled the good one first which is something that was purchased.
All right.
So first things first is we're going to
finish our transaction.
So we need to let the service know that we're done.
So we're going to S.K. payment.
Q default finish transaction completes a pending transaction and we're going to pass in the transaction.
Okay.
Maybe you're thinking well why are we manually finishing this doesn't Apple know when it's done.
Well Apple knows when it's done but what the important thing to know is let's say it failed and we didn't
want to finish it quite that we could actually reprocess it again and maybe even do two or three attempts
before we actually consider it done.
And we say hey users nothing we can do.
So in this case we're just going to finish it because it was successful which is great
if let Prod. ID the product ID equals.
Interesting actually.
So how do we know which product to actually do.
So what we should actually probably do here is say if transaction payment product identifier there it
is.
So if the product identifier that's coming through this transaction equals the remove ads product identifier.
Then what do we want to do.
We want to make the purchase right.
So if payment dot product identifier goes in a purchase ads what do we want to do.
Well we don't have the functionality yet in place for removing the ads and getting them off the screen
etc. but we'll leave this here.
This is our success block here so let's go actually write that code now.
So let's think about this what we want to do is store something in the user defaults that lets the app
know that we should not show ads so every time it loads we can remove those ads accordingly at runtime.
So let's just set a boolean here or a value of some kind.
I mean so user defaults not standard set.
And what do we want to do we want to set a value for a key.
Let's go ahead and set true for key and let's just do the in a purchase ID.
So what we're doing here is saying if this key exists.
OK.
So if it's true for a specific name it means it's there it means we should not show the advertisements
that's all.
We're just setting a boolean here and it's really as simple as that how you work with in-app purchases
is completely up to you how you manage them in your app and how you work with things move things around
and show and not show it's all up to you 100 percent.
You've got to use the logic to figure things out.
OK so it's cool.
So let's go ahead and just finish the transaction actually in both these cases here the felt and the
restored.
We're not going to do any retries or anything like that.
Cool.
So we finished the transaction make the purchase.
Okay great problem.
How do we how does our view controller know first off when this is done.
And we haven't even started an actual payment to be processed yet we haven't clicked on a button and
said start the process or purchase the purchase or remove the ads.
We haven't even written that code yet.
And also then there's the notion of when you do press that and has to go to the server How does the
view controller know when it's succeeded or failed.
We don't have any way of letting the controller know.
So we're going to need to do that as well too.
And so let's call this video done and then we'll do that in the next video.
So in this video we what we done was we actually made it so you could purchase the product.
And then we handle what happens when it is purchased which is storing a boolean that has been purchased
so when the app loads the next time we can remove that advertisement.
So good stuff here.
Let's move on to Ford.
Mark Price of slopes dot.com.