Hey everybody what's going on this is Caleb with slopes dot com and in this video we're going to set
up restoring our purchases that we already bought which is really cool it's a handy feature even if
you know you don't have it and someone wants to buy you're in that purchase that's non consumable.
The apps are automatically going to say hey you've already bought this habit for free but it's helpful
to have restore purchases in the instance of an app where you have maybe like 30 different in-app purchases
and maybe they've bought seven of them.
They don't have to go through and individually buy them all.
You can set it up this way so that they could you know restore them all at once which is really neat.
So you pull open your X code project and we're going to go into the IAP service.
We're going to go ahead and close this out and we're going to basically set up our function for one
that's going to restore purchases.
Super super easy guys.
And then we're going to set up some of the other things we need to like the payment Q for our restored
case we're also going to set up a function here called restored and that restored function is going
to send a notification.
It's also going to set the value for our user defaults.
So let's go ahead and let's dive right in.
The function is unbelievably easy and we're going to go ahead and put that down here.
Going to say phunk Let's move that more in the middle phunk restore purchases like that and then we're
going to call S-K payment.
Q default and then datt restore restore completed transactions.
That's it.
Seriously that's all we need to do.
And when we call this it's going to go ahead and call us through the payment.
Q So it restored is a little bit different because we wrote comp. here because we needed to do specific
things after we complete but restored is going to do something a little bit different it already has
its own delegate method.
That's a part of S-K payment transaction observer that we can utilize that gets called after a Restore
is successfully finished.
So go ahead and just call restore and right here look at this payment to restore completed transactions
finished push enter and this is called whenever a restore successfully happens.
And so what we're going to do is we're going to send a notification for status check it out restored
with identifier of nil because we don't need the identifier for this because it's just going to restore
everything we've already purchased.
And then if it's restored remember the purchase is hide adds that's the non consumable one that gets
restored.
So therefore we need to go ahead and call set non consumable purchase to be true.
All right.
And guys I'm not kidding that's about all we need to do here.
One one thing sorry that I did forget.
We do need to call S-K payment.
Q finished transaction because it is a transaction nonetheless.
But then it's going to go ahead and call this delegate method.
Send the notification wherever we need it and then set the non consumable purchases to be true.
Now at the moment we're not sending a notification anywhere.
So let's go ahead and head into storefront Visi where our restore button is.
And let's set that up first actually.
So restore button if you remember is the question mark button in the top right hand side of the screen
show you on my little simulator here.
This button is the restore button.
And in order to actually use that we're going to go ahead and present an alert Visi whenever that button
is pressed.
Giving them an option do you want to restore or not.
So they have a choice.
So let's go ahead and do that.
Let alert Visi equals UI alert controller.
Go ahead and instantiate it and we're going to choose the one with a title message and preferred style
the preferred style or sorry the title is going to say restore purchases.
With a question mark.
The message is going to say do you want to restore any in App purchases you've previously bought maybe
you previously bought.
Or maybe you've previously purchased.
That sounds a little more professional and the style normally you would do alert which is like a pop
up alert but I really like action sheets.
I think they're pretty cool.
So I'm going to use action instead.
And now we're going to actually create the action that happens whenever the button is pressed.
So go ahead and type let action equals UI alert action.
Now go ahead and instantiate that as well with a title style and handler and the title.
This is what the button is actually going to say.
We're going to say restore the style is going to be default and for the handler.
Go ahead and select it press enter.
And we're just going to say Action.
OK.
We don't really need what comes back from that.
But inside this closure we can perform certain behaviors and so the one we want to perform is from our
pre-service instance that restore purchases and that's all we need to do to restore our purchases successfully.
So that's one thing.
Let's go ahead and create a cancel action as well so we can cancel and just do that by typing let cancell
action equals you alert action.
Whoops.
The title is going to be cancel.
The style is going to be cancel and the alert handler is going to be nil.
You'll notice it's optional.
You don't have to use it.
And that makes it perfect for canceling.
Next we need to add our actions to our alert Visi so call alert Visi add action.
Add the first action like so then call it again alert VCM at action you can add an array of actions
which is kind of annoying but anyway cancel action.
Then at the very end we need to present
are alert you see animated.
True and completion is nil.
Now when we push our button it's going to present this alert Visi And if we push on the restore button
we're going to be able to call our restore function which if you remember let's go into it let's go
find it.
It's going to call restore completed transactions.
It's going to run through the payment queue.
It will finish the transaction and then call payment restore completed transactions which is going to
send our restored notification and set the non consumable purchase to be true which is really cool because
it's going to let us know set our view to be hidden by the time that this even loads when it's restored
it sets that boolean value which then in detail Visi is set here.
So when we present the view it will show up as hidden which is really neat.
Now we do need to maybe present a an alert Visi that says Hey thanks for that your purchases are restored.
And so to do that we're going to go ahead and set up an observer for that IAP service restore notification
and then whenever that is notified we're going to present an alert visi.
So go ahead and type notification center notification type notification center that default dot our
observer and we're going to say the observer is self the selector will get back to the notification
is notification name and for the name pasan IAP service restore notification.
The object can be nil and the selector we're going to make a function that presents an alert Visi saying
hey your purchases were restored.
So let's do that down here.
Let's go ahead and just.
Well maybe not below the actions.
Let's do it right below.
Are you a collection of your stuff.
Go ahead and type at obedience at O.B. J.C. Funk show alert.
And we're going to say let alert Visi we're kind of doing the same thing we probably should put this
in an extension but it's ok we can just do it this way.
The title will say success and the message will say your purchases were successfully restored.
The style is going to be an alert just to pop up.
Let's go ahead and let's add an action.
Action equals UI alert action.
And we're going to instantiate that with a title the button.
Right that's the action.
Just going to say OK the style is going to be default and the handler is going to be nil we don't care
what happens.
We just wanted to go away.
Now add alert VCM add action and add the action then present alert b.c.
And you know what.
This is not really a view controller.
I mean it is sort of but let's just call it an alert.
Makes a little more sense.
I think that's good.
Same with this it's not really a controller.
It's just an alert that pops up over the view controller.
So we'll just call it an alert and we'll present dealer.
I mean technically speaking if you dive in deep you can see that and it inherits from you and you controller
but I don't like calling it if you control or even is.
So now when we push our restore button we should restore purchases.
If successful we should be able to you know show an alert which is neat and we're just going to go ahead
and I guess we can call our selector here poun selector and let's actually rename this function let's
just say show restored alert and we can call that here.
So when we successfully restore it will pop up and say hey you restored.
Let's go try it.
And you know what.
Oh sorry guys.
Before we build it I'm going to cancel this.
I do need to delete it from my phone because I've already bought the in-app purchase meaning that the
ads will be hidden and there's no way for us to tell if the restore worked.
So I just deleted it.
I'm installing it now.
And as soon as it pops up I will show you in quick time.
Here we go.
OK.
So let's take a look.
The ads are there.
OK that's good.
Now when I tap this it should show our pop up there we go.
Do you want to restore an in-app purchases tap restore your purchases were successfully restored.
Press OK and let's go check.
Oh my gosh.
That's amazing because I've already purchased the hide ads and at purses it shows up right there.
Boom.
Oh my gosh this is amazing guys.
So this is this is done.
We have now properly set up consumables and non consumables in the same app with iTunes Connect with
everything we need.
We have an awesome IP service that processes all of it does all the cool stuff.
Oh my goodness guys.
This is incredible.
Awesome awesome work and congratulations.
You now have a great foundation in in-app purchases consumable and non consumables.
Stay tuned for the next target topic which is going to be all about auto renewing subscriptions as a
form of in-app purchases.
Very very cool.
We're going to do all kinds of amazing stuff like a receipt validation to prevent yourself against jail
broken phones who have one of those in app purchased tweaks that let's let them kind of hack them onto
their phone.
We're going to be able to actually upload the receipt that's generated and verify it against Apple's
servers.
Otherwise it's not going to work.
Super cool stuff to help you as a business person who's developing an app.
This is your money maker.
You can't have people stealing your in-app purchases.
So we'll make sure it doesn't happen.
Amazing work guys.
Thank you so much for watching this course.
And I just wish you the most success in integrating in-app purchases into your app.
This is Caleb with Debb slopes dot com and I'll see in the next target topic.