Hey everybody what's going on this is Caleb with Debb slopes dot com and in this video we're going to
go ahead and fix those problems.
We were just having in the previous video where basically we were not able to show the changes made
in a subscription when we wanted to be showing them.
So that's a problem.
Let's go ahead and pull open our Esco project and let's troubleshoot this let's figure out what the
reason for our problems are.
So it appears that requested finish is not being called when the actual purchase is being made and I'm
trying to figure out why that is.
But I think what it boils down to is that we are calling finished transaction before we're actually
completing.
And that's I think the problem we are telling it to be done before we're done using the data.
So let's go ahead and let's actually put that beneath complete.
And we're also going to go ahead and put failed beneath the notification just to be consistent.
So what we can also do is think about what happens if payment is deferred.
Let's say someone can't pay.
We're going to go ahead and call a finished transaction there as well.
And you know while we're purchasing why don't we go ahead and handle that as well.
We're called.
Print.
And we'll just say purchasing can't type purchasing data.
And that will basically just print out when the user taps the buy button until they either purchase
restore fail or defer.
So that's being called.
But you know what I don't really like how I'm not sending a notification when I actually buy it because
that that implies that our function here is going to get called every single time and we want to make
sure that it gets called.
So let's go ahead and let's actually modify complete to be the way it was before let's call case IAP
mealtime monthly sub.
And then what we're going to do is we're going to actually call the same code from this function right
here for a successful subscription because at that point we have succeeded as well.
Now let's go.
And let's type that.
Here we're going to paste it and then we're actually going to break.
Okay.
So there we go.
Beautiful.
We have an executable statement we're calling true.
We're setting that boolean.
We are also setting the non consumable purchase to be true.
So go ahead and build and run.
Let's go see if this helps to fix our problem.
Our subscription should be expired from the last video.
And let's go ahead and check this out.
It says subscription active and when we loaded up the app look at that it actually bought a new subscription
for us.
So that is not a good thing.
That is not doing what it's supposed to be doing very uncool.
So we need to fix this.
Let's go ahead and let's go into storefront Visi let's think about what we could be missing here.
What are we missing.
Oh we are missing a very important thing.
We are adding observers for these notifications but we are never removing them.
That is a huge problem.
I am surprised we have not noticed that until right now.
Okay so you don't we're actually going to do.
We're going to call and net and whenever this view controller is DNA sized we're going to just remove
all the observers on this view controller.
So go ahead and call notification center dot default dot remove observer and call self.
OK.
So now whenever this view controller is d initialized it's going to remove that observer.
And that way we won't accidentally get you know observers that are just kind of locked in memory for
when the app opens again so called DNS and remove the observers here guys.
This is really important I'm surprised we didn't do this.
We also need to call this on detail Visi like so let's go ahead and put it right beneath you will disappear
like so.
And oh you know what it looks like we are removing it from from view will disappear.
Let's actually we don't need to call view will disappear it will just do initialize it because whenever
the view controller goes away it's initialized by Swift.
So we can put that there.
Let's go ahead and build and run.
OK let's go ahead and check out what we did here.
And let's see if this helps.
OK.
Subscription is active.
That's true because we still have about two minutes left.
Being 841.
So we're going to just go ahead and hang tight while the subscription is still active.
It does appear that it is sending through OK I can go into my different view controllers.
The ads are gone.
Ok that's cool.
So yeah I think that this will help removing the observers from storefront Visi because that is actually
that might be the source of our issue here is that the notifications are never being removed and they
might be getting stuck in memory which could be a part of our problem here so let's just verify that
everything is good if the status comes in as true.
We're updating the label.
We're going to update the the label to read if it's not true.
You don't want.
Why don't we do some more fun stuff.
Why don't we say something like self-direct.
Well and that self may be viewed background color.
Let's change the background color when we are subscribed let's do like a dark mode kind of thing.
So let's go ahead and set it to be like a dark gray.
That looks pretty cool.
And why don't we set the collection view.
Background color to also be that dark gray using a color literal cool.
Subscription is active.
Ok that's cool.
We're going to we've got to use self since we're in a closure.
Do it again self and copy this paste it below.
And we're going to set this back to white K when we're expired we're going to have normal light mode
and then when we are subscribed we'll have a dark mode.
Let's build and run just to see that while we're waiting for a subscription to expire and you know what
it actually may already be expired.
So that's fine.
Okay.
It's expired.
The background is white.
Very cool.
Let's tap on purchase and let's enter our password.
Test test one two three is thing on press OK.
And let's see.
Here we go.
Confirm purchase confirms subscription and we're waiting to see if it actually gets confirmed.
Boom.
Oh my gosh do you see that subscription is active.
That is the coolest thing.
It worked.
Look at that.
And now that our subscription is active we can actually go in and we can view this.
Everything is good to go.
We are not having issues with our subscription anymore.
If we build and run.
Let's go back and let's take a look.
We should still have our dark mode as soon as it loads updating subscription active.
Wow that is so cool.
I do not like the food Zyla is still gray.
So we're going to fix that really quickly.
But guys we have a subscription when it expires.
We can actually see how long we have we can know when it expires when we reload the app that subscription
is going to go ahead and show as expired and it will turn the user interface back to white.
Our ads are there earn our ads are not there as part of our subscription.
Wow guys this is really really cool.
So let's go ahead and head into main storyboard and let's set up our food Zyla label to turn white.
When we want it to let's just right click and drag from that label and we'll just say food Zilah label
go back into the store front and in our handling function.
Go ahead and call.
Let's just say self.
Food Zyla label dot text color and we're going to set it to be a color literal of white when the background
is dark and then we're going to go ahead and change it back to the same dark gray color.
Yeah.
This one here when the subscriptions expired so let's build and run.
Let's see how our dark mode looks.
So cool guys.
Awesome awesome awesome.
So a building run here let's see.
Subscription is active and food Zyla turns white that looks so cool.
And guys this is amazing.
Our subscription is working.
I do want to add one more thing that will allow us basically just for testing purposes.
I found that it was really really helpful to have a print out that showed how many minutes I had left
it was just helpful to see.
And so what I'm actually going to do is inside our function where we check if it's active which is in
our IP service way up here.
What we're going to do is we're actually going to go ahead and print out the expiration date and then
we're going to use something called time interval since now which basically gives us the amount of seconds
until the expiration date and then we're just going to divide it by 60 to get the amount of minutes.
So go ahead and call debug print and then we're going to actually print out time remaining time remaining
and then we can go ahead and pass in the expiration date Dot time interval since now which says the
time interval between the date value and the current date and time.
So it tells us how long we have until this subscription and then basically we're just going to divide
that by 60 and print it out and that'll tell us how many minutes we have.
Now it's saying value of optional type not unwrapped.
And that's true.
So we need to think about how we're going to handle this.
You know what at this point we have reloaded the expiration date so we know it's going to be good.
So we can actually go ahead and put this in parentheses and force on rapid like so.
So yeah build and run that and the arrow will go away.
We do know we will have a date and we can you know print out the time remaining so.
Now let's check it out.
Awesome time remaining.
And well let's add minutes to the end of that and it's like so and it'll say X minutes.
OK so that's pretty cool 1.3 minutes.
Let's just double check to make sure that's accurate 1.0 minutes it expires at 4:48 and 49 seconds that
that could be about a minute.
Let's say it should turn to 8:48 soon.
And yeah that's cool that's going to let us check how many minutes we have BuildOn run.
Very cool.
And yeah point seven three minutes yeah that's about right.
So in point seven three minutes whatever that means we're going to go ahead and verify that our subscription
goes back to non active.
And I just realized you know we could do we can call it subscription active from view will appear so
that if we were to close out while this description expires Oh you know what you should see that if
we were to open this and then close out like so we can call is subscription active and we could you
know act accordingly.
So let's go ahead and head into storefront Visi Lets call view will appear called Super view will appear
and passen animated then we can go ahead and call IAP service instance dot is subscription active and
for the completion handler we can just stay active and we dont actually have to do anything in that.
We just want to call it now we could make the completion händler optional.
I'm just not going to.
So let's go ahead let's build and run let's just verify that our subscription is good to go.
And actually I think it's expired at the moment so we should see inactive and yes as you can see the
subscription has expired.
If I open up a screen you can see the ads are back but watch what happens when I close it.
Now when the view appears it tells me how many minutes are remaining and that means my subscription
is not active.
So what I can do is I can go ahead and resubscribe tap confirm purchase confirm subscription and my
subscription is active.
Now if I open it the ads are gone.
If I close it it gives me the time remaining.
So yeah guys our subscription is valid.
We have about four minutes left and this is this is it guys this is how you do auto renewing subscriptions
and AOS we're able to subscribe when it expires.
The user interface changes the feature the features change and we're able to pretty much control whatever
we want using our IAP sub info has changed notification.
Very very cool.
There is our subscription interface again and our ads that are gone which is cool.
So let's go ahead and let's head over to the very last video where I'm going to give you a little challenge
to make this app even better.
So let's head over there now.