Hey everybody what's going on says Caleb with Dev slopes com and in this video we're going to talk about
how do we handle a successful purchase.
How do we handle a failure payment deferred et cetera et cetera.
How are we going to manage that and how are we going to notify our application that these certain things
have happened.
We're going to dive into it right now and let's pull open our project and let's check it out.
So to begin we're actually going to need to go into our IAP service so click on that.
And then inside of the IAP service what we're going to do is we're going to scroll down to where we
basically handle our payment request.
So scroll down all the way here to where we basically conform to S-K payment transaction observer.
Now inside of the payment queue we go through a switch for the transaction state.
So let's say that I attempted to purchase something and the purchase failed maybe I canceled the purchase.
Maybe my phone crashed or something.
What it's going to do is it's going to switch through it's going to go ahead and go to Case whoops case
failed.
It's going to finish the transaction and send a notification for failed and that this is a very important
function.
I'm going to show you this now.
So it's down at the very bottom of the View Controller.
And here we have send notification for status and that comes from purchase status which is another enumeration
now purchased restored and failed are three different ways that we can basically say hey we purchased
something we restored something or we failed something but we're going to actually add a fourth case
here and we're going to add case subscribed because that's another thing.
If we purchase the food item that's one thing but subscribing is entirely different.
So we're going to set another case here to be able to differentiate those different things.
Now let's go back to IP service and you're going to notice if I build this it's going to say this switch
is not exhaustive and that's because we're not handling what happens in the instance of a subscription.
So go ahead and call case dot and look at that subscriber shows up put a call in and press enter.
And now this is where all the good stuff is going to happen.
For now we're just going to break but we're going to go ahead and add a custom notification here that
will be posted as soon as a subscription successfully happens.
OK.
So you'll notice that there are some constants here that are saved IAP service purchase notification
IAP service restore an IP service failure.
Now if you go into constants and take a look you'll notice that I've just saved these here to have the
same string value.
Now we're going to go ahead and create another one for what happens if we subscribe.
That means our subscription information has changed.
Either we have subscribed we haven't.
Maybe our subscription expired et cetera et cetera.
So go ahead and type let IAAPA service subscribed status changed notification.
That's super long Let's just say.
IAP sub info changed.
That's a little better.
Press equals and go ahead and create a string of the same name IAP sub info change notification and
let's leave it at that.
So back into the IP service we're going to go ahead and call notification center like so default will
access the default notification center and we're going to post a notification.
Okay so go ahead and type post and we're going to choose the one with the name and an object.
Okay.
So go ahead and type an s notification
name and then inside the parentheses we're going to call IAP and it's going to be not available because
we've not built it yet.
IAP sub There it is.
IAP sub info changed notification.
Now the object is actually going to be a boolean.
OK.
And the reason for that being is we're either going to say are we subscribed or are we not.
Or maybe is our subscription valid or is it expired.
And so what I'm going to do is I'm actually going to modify my function here because if you look we
can pass in a status which is mandatory and an identifier which is optional.
The reason for that is because if our payment is restored it doesn't matter what it was it's just going
to restore everything that we've already bought.
If it's failed it doesn't matter what object it came from because we just need to know that the payment
failed.
If we're subscribed we need to know is it valid or is it expired.
And so we're going to use a boolean but we're not currently passing in a boolean.
So we're going to add a new property here.
So we're going to say send notification for status with identifier or Boullion and we're going to use
bhool as an internal parameter of type Boolean.
OK.
Now pass in Bhool as the object here.
And that is what we're going to use to to post our subscription notification.
It's pretty cool.
Now you're going to notice some issues.
It's going to say that we are missing some arguments and we are because we're calling send notification
right here.
Now of course we are calling it where we are making our purchase and this is another thing we need to
think about when we send the notification.
This is for only a purchase but how are we going to differentiate between a purchase of a subscription
and a purchase of just like a meal or hide ads.
To do that.
We're actually going to get rid of this function and put it inside the complete function.
So go ahead and cut this like so and then go find the complete function like so.
OK.
Check it out inside.
Complete.
We're going to go ahead and use our switch to find either our hide adds ID or our subscription ID.
So I'm going to add a new case right at the top case IAP mealtime monthly sub.
OK because remember these are all for the product identifier on iTunes Connect.
So go ahead and put a colon press enter.
And what we're going to do is we're going to go ahead and basically copy and paste in what we just copied
from above which is send notification for status purchased.
But remember this is a subscription.
And we have a special identifier just for that.
We have dot subscribed.
OK Bill to make sure that that's good to go and it's going to say we're missing a parameter which we
are so click fix.
And remember for our mealtime monthly sub if we are successful what we need to be doing is we basically
need to pass in.
True.
Right because we are successful we did buy it.
And so then it's going to go ahead and pass in that bullion as the notifications object which is awesome.
Now at the very end since we're in a switch we need to break so that it knows to get out of it and then
we also need to think if we are purchasing something we need to send a notification that this meal has
been purchased so we can put that in IAP meal ID.
And this one works right because we are sending it in as a purchase.
Very very cool.
So that should be good.
We need to add our identifier here.
And for this one we don't need to pass in a boolean.
So let's just say nil we don't need it.
Very very cool.
So let's think what do we need to do next.
We need to make sure that all our errors are gone.
OK so click fix for restored.
This is what is called After we successfully restore everything.
We're going to send the restored notification with no identifier and no booleans so passen Nelle we
have to fix this.
It's going to say nyl is not compatible with argument type bool.
Why is that.
Oh that's because we didn't make this optional whoops that should be optional and this may ask us to
force enwrap.
I don't believe it will.
OK.
So missing argument let's add it in let's see failed does not matter.
So put in nil and now it looks like we are doing pretty good here so we make a purchase.
The purchase is finished we call comp. And based on that we send a notification saying hey we successfully
subscribed.
Very very cool.
But there's something else.
Remember a subscription had a perk it automatically hid the ads for us.
So what we're going to do is we're going to also call set non consumable purchase to be true when we
make a subscription.
So go ahead and actually we're going to copy this like so and we're going to go ahead and paste it beneath.
So we're doing both We're sending a notification for scription.
We're also setting the non consumable purchase to be true.
OK.
Very very cool.
So that's awesome.
Let's think.
Is there anything else we should do we may have to come back and look at stuff later.
But I think that's pretty good.
When we when we subscribe we're going to send a notification we're going to set that non consumable
purchase to be true.
And we're basically going to be able to hide the ads when we subscribe which is very very cool.
So let's go ahead and let's set up an observer for this notification so we can see if it's actually
working when we make a subscription.
So go ahead and head into storefront Visi because that's where our subscribed button is and click on
it.
And then what we're going to do is in view did load We're going to set up another notification observer
here.
Now this one is for when a purchase is restored and as you can see when show restored alert is called
it presents an alert basically saying hey your purchases were successfully restored.
We're going to basically set up a function that will be able to determine whether or not we are valid
in our subscription.
So to do this let's create another observer by typing notification center default.
Our observer can we're going to choose the one here with an observer selector name an object and the
observer is self or this view controller the selector will come back to in a second.
The name we're just going to use an S notification name.
But then in parentheses following it we're going to put in our subscription notification because that's
the one that gets posted when we subscribe.
So type IAP sub info change notification and the object we can just say it's nil because that will actually
be passed through in the next notification.
So for the selector we need to write a function that will do a certain behavior or perform a certain
action whenever that notification is posted.
So let's actually write that function now by typing phunk subscription status was changed.
OK.
And basically we need to think what are we going to do here.
We get a value passed in.
If you remember when we subscribe we pass in a boolean either true or false.
And that is accessible to us in the notification that's sent now to access that what we can do is we
can go ahead and set up a parameter of type notification.
So to do that I'm just going to put an underscore because the name doesn't really matter.
But I am going to give an internal parameter of notification.
OK and that's going to be of type notification like I said.
Now in order to actually access the object of bullion that's passed in we can go ahead and create a
constant that pulls out the object from the notification.
So to be safe I'm going to use guard Latt by typing guard let let's just say status equals notification.
OK the one we passed in object.
Check that out as a boolean.
OK but since this is a guard but we need to provide another option so else meaning if that doesn't work
if we don't get a value we're just going to return get out of this function.
Keep us from crashing.
OK.
So that's great.
But otherwise we will have a boolean value in this status and we can basically create two conditions
if status is true do one thing if status is not true do another thing.
Let's set that up if status is true.
Else.
And this is where we're going to perform actions for success with let's say active subscription
and this is where we're going to go ahead and perform actions for expired subscription and we'll get
to that in a moment.
But for now let's see what can we do we could print.
Let's go ahead and call de-bug print and we'll just say subscription valid.
OK.
And for expired we'll say subscription expired and there is that.
That's our function.
But we're getting an error up here or we will if we try to build saying that we have you know a blank
boiler plate code still sitting in there.
And so to do that we're going to type LB selector.
And if you know anything about selectors we can just pass in the name of a function and that basically
sets it up to call a certain function for us.
So that function is subscription status was changed.
Press enter and the cool thing is that remember that this parameter here is of type notification.
It'll automatically get passed in thanks to notification center.
Now it's giving us an error saying that the argument Selecta refers to instance method that's not exposed
to Objective-C.
Click Fix.
And what that's going to do.
Whoops there we go.
It's basically going to add at BJC to the front of our function just exposing it to the object C code
that X code needs to process this.
So that was a lot of code.
Let's go ahead and build and run this.
And let's go see if this fixed are not fixed but let's see if when we subscribe if the notification
is successfully sent.
Ok so I'm going to go ahead and build and run this.
I'm going to go ahead and show you my simulator device.
And here we go.
So you can see this I can see this when I tap on the dollar sign like so it asks for our password so
let's type that in.
Test test one two three exclamation mark
OK so it says subscribe to meal plan this subscription will automatically renew every month for forty
nine ninety nine.
Press continue.
And it says this subscription will continue unless cancelled in settings at least one day before a subscription
period ends.
Press OK and when we press OK it should purchase OK so when we purchased it boom we finished the transaction.
We call complete complete is here.
OK.
It will be IAP monthly sub meaning we'll send our notification and we will set our purchase to be true
so the ads should go away now send notification for subscribed is going to post our notification which
in storefront Visi we have an observer for that notification.
When something is sent using that notification we're going to pass it in here.
If it's true meaning if the subscription is true we're going to get subscription valid.
Otherwise we'll get subscription expired.
So go ahead and push.
OK.
And let's see if we get a nice little print statement here.
I pushed OK sorry you couldn't see that on my screen.
But all that happened was it popped up saying your purchase was successful.
We get a notification saying subscription valid check that out.
That's cool.
And we also got our success message from our IP service which is right here purchased was successful.
That is so cool.
And now if I rebuild my app you'll notice that my subscription is still valid.
Now if I pull up one of the controllers you'll notice the ads are gone.
It successfully saved so remember we set our non-consumer consumable purchase to be true and that basically
went into user defaults and it set true for our non consumable purchase was made.
And that's going to basically allow us to hide those ads.
OK because in detail we see we have a variable called Hidden status that gets its value from that exact
same user defaults boolean and we use that to basically determine whether or not the ads should show
or hide.
OK that's pretty cool.
So let's think what can we do next.
What can we do to you know show that this is an ad subscription that we have a valid subscription.
Let's go ahead and let's think I'm going to actually go to my storyboard and we're going to brainstorm
what we can do to actually show whether or not this is a valid subscription.
And I think what I want to do actually is I want to put a label here that says subscription valid or
subscription expired.
So let's drag that on really fast.
You can't type you label drag it on and make sure that it does not touch the collection view.
But it goes below like like that and I'm going to drag it.
So it's nice and big 50 points high.
And let's go ahead and let's pin that to the left bottom and right and give it a fixed height of 50.
There we go Center the font and change it to Avenir next.
That's my favorite font.
I mean you can use whatever you want.
But anyway and I'm going to bump it up to 24 and set it to be bold.
OK.
Now this will say updating.
And basically depending on whether or not our subscription is active or expired we're going to set it
so that it changes the font and color accordingly.
Now Black is pretty intense of a color here so I think what I'm actually going to do is I'm actually
going to choose a nice dark gray.
That's a little less intense.
I chose tungsten or if you want to know it's just six threes.
That's pretty cool.
So there we go our updating label is here but we should connect this to The View Controller so that
we can properly update the label and we need to so open the assistant editor here and go ahead and pull
up to the top right click on the label and drag it here and we're going to say subscription subscription
status label.
Press connect.
And now we have access to that label.
It will show updating by default.
And now we're going to move on and head over to the next video where we're going to start customizing
the experience of our app as well as preventing users from stealing this subscription.
If you know anything about the jailbreak scene with iPhones there are ways to get around paying for
in-app purchases.
But we're going to make sure that people cannot do that by verifying the receipt with Apple.
Very very cool.
Let's head over to the next video and let's start doing that right now.