Hey everyone welcome to the last exciting video in working with in-app purchases.
My name is Mark price.
In case you forgot and we are going to finish this up so what we want to do is first get our restore
in-app purchases button on our storyboard.
And again the point of restoring in our purchases is in the case that someone deletes their app and
they re install it your app and reasons reinstalls it.
They need to be able to get their purchases back.
And this is an apple requirement if you do not implement restore in-app purchases your app will be rejected
of course with consumables or excuse me non consumables with consumables that doesnt matter.
But if you have a one time non-consentual purchase you have to restore it or Apple will reject you.
So put a button up here at the top and take out the word button like so and we are going to go ahead
and put in the restore button here and I'm going to pin it to the top 10 from the right give it a fixed
width and height just make it nice and easy and we're going to open the assistant editor and just simply
make an IB action done this before no big deal.
Put it about this one button and control drag and this is going to be an action and we're going to call
this restore.
But in pressed close the assistant editor.
Let's look at our code here.
Store button press cleanup.
And what we're you going to do is we're going to have a function in our purchase manager so let's pretend
the function exists and we're going to say purchase manager instance dot.
Well this is great at first because we're gonna need to put a callback in there.
It's all good.
Let's go to purchase manager and do that now.
So we'll do the same thing as this but we'll change the name of the function and we're going to call
this store purchases like so.
OK.
And now what we can do is we can go back to the home PC and call that function so will say purchase
manager for instance restore purchases.
And just like before we'll create or little completion handler Here success and there we have it.
So when that's done we'll be able to restore.
And of course if it was failed you could say well you don't have purchases to restore things like that.
So we're just kind of handling all the cases there.
So what do we need to do.
Go back to our purchase manager and actually just write the code to restore it and purchase so purchased
manager or store purchases.
And if Eskay payment you can make payments you always check that.
And what did you say else.
Okay so if we can make payments and then let's go ahead and store transaction complete equals on comp.
let's store that callback handler.
Okay.
And then if it fails or if you can't make payments we'll say on complete and we'll say false.
So they can't make a payment so we obviously can't restore the purchases which is fine and then we're
storing this here.
So let's go ahead and say S-K payment Eskay payment.
Q default at looks at herself as an observer let's say self and then let's say S-K payment.
Q That default restore completed transactions.
Wonderful.
So as similar as we did before we're adding ourself as an observer for this particular payment or transaction.
And then we're calling the restore completed transaction which is going to go to Apple and is going
to do all the work under the hood for us to see which transactions need to be restored if any at all.
Okay.
And so back down here in our restored section now we can just finish this up.
No big deal.
We're going to say if transaction payment type product identifier equals IAP remove ads.
So if it equals the remove ads all we have to do is what we've done before.
Saving the transaction with the user default so we can just copy this line of code here to save that
purchase like so.
OK.
And then we can just call the callback handler.
So transaction what's already here actually right here.
That's cool.
That's yeah I like that a lot.
Let's just leave it there.
We'll leave it there as is.
Because let's assume that there was more than one in that purchase.
You know we would want to do all of them check all of them and then call the transaction complete.
So that's fine.
I like this we just setting the standard defaults to true on this completion Hendershot and then we
go back here to the home visi.
OK.
And what we can do is what we kind of did before up here is we check this.
In fact we can call it we can put this to a function or make it reusable.
So we'll call this function phunk set up ads.
Okay.
And we'll just cut this out of here like so pasted here and then will call set up ads and then here
we will say if success sort of adds We don't know if it fails we don't need to because the ads are already
there.
So if it was successful it set up the ad so it's going to go through here do the check.
Hey does it exist.
If so remove these otherwise load the advertisement which is just what we want to do.
Awesome.
Very very cool.
So let's see if it actually works here.
Got all of our logic in place.
It seems like so what we need to do is actually delete the app from the phone and then reinstall it
and run it with the same user and see if it actually works.
So let's do that now.
So I'm going to delete these here if I can get past the 3D touch which it did.
So those are deleted.
So right the users uninstalled your app.
They later decided you know what I miss the app.
I want to install it again but I Wylma in it purchases what do we do.
So let's go ahead and run it on the phone again.
It does not work on the simulator
it's set up self.
Here we go.
Now let's run it and the app is running.
And of course we see advertisements as expected.
And so what should happen is when I press that button at the top right it should prompt me.
And then my ads should go away.
So press the button.
Well that was fast a little bit too fast don't you think so.
So we let's make sure nothing crazy is going on here.
So we clicked restore purchases.
We went to the purchase manager K and we typed restore purchases.
If we can make payments great.
Otherwise uncomplete was false.
OK.
And so uncomplete our code here let's check it out.
If success is true we'll set up the ads.
Well it wasn't true there so we're good there.
And then we went through here to add and restore the completed transactions and went through here restored.
We finished it.
And if the payment product identifier is removed the adds then let's set it to true.
Wow that was fast so it all worked.
That's pretty cool.
I thought it would take a little bit longer but it worked.
It worked without even prompting for a password or anything like that which is really awesome.
Apple knows which device it is under the hood and made it work for us.
So there you have it.
You've now learned how to work with in-app purchases you've learned how to set up your accounts on iTunes
and developer to Apple dotcom how to get your apps to sink to that.
Don't forget to turn on the capabilities on your project.
Clicking the capabilities tab here you learn how to integrate Google AdMob and how to get better at
showing.
And then you learn how to manage your code so when you make that purchase you actually implement the
feature and how to restore the in-app purchases in the case that an app is deleted and then re-installed
a lot of good stuff here.
We're only scratching the surface though and in a purchase purchases you can do a lot more with subscriptions
and non and consumables as well too.
But enough to get you started.
So that's it for now.
Marc price dove slopes dot com.
See you next time.