1
00:00:07,120 --> 00:00:11,860
Hey everybody what's going on this is Caleb with Debb slopes dot com and in this video we're going to

2
00:00:11,860 --> 00:00:13,710
go ahead and fix those problems.

3
00:00:13,710 --> 00:00:20,560
We were just having in the previous video where basically we were not able to show the changes made

4
00:00:20,560 --> 00:00:23,380
in a subscription when we wanted to be showing them.

5
00:00:23,380 --> 00:00:24,460
So that's a problem.

6
00:00:24,460 --> 00:00:28,900
Let's go ahead and pull open our Esco project and let's troubleshoot this let's figure out what the

7
00:00:28,900 --> 00:00:31,640
reason for our problems are.

8
00:00:31,840 --> 00:00:40,990
So it appears that requested finish is not being called when the actual purchase is being made and I'm

9
00:00:40,990 --> 00:00:42,940
trying to figure out why that is.

10
00:00:42,940 --> 00:00:48,490
But I think what it boils down to is that we are calling finished transaction before we're actually

11
00:00:48,490 --> 00:00:49,340
completing.

12
00:00:49,510 --> 00:00:54,160
And that's I think the problem we are telling it to be done before we're done using the data.

13
00:00:54,160 --> 00:00:57,830
So let's go ahead and let's actually put that beneath complete.

14
00:00:57,970 --> 00:01:03,240
And we're also going to go ahead and put failed beneath the notification just to be consistent.

15
00:01:03,310 --> 00:01:07,560
So what we can also do is think about what happens if payment is deferred.

16
00:01:07,570 --> 00:01:09,300
Let's say someone can't pay.

17
00:01:09,340 --> 00:01:13,230
We're going to go ahead and call a finished transaction there as well.

18
00:01:13,360 --> 00:01:17,400
And you know while we're purchasing why don't we go ahead and handle that as well.

19
00:01:17,420 --> 00:01:18,340
We're called.

20
00:01:18,360 --> 00:01:18,930
Print.

21
00:01:18,970 --> 00:01:24,720
And we'll just say purchasing can't type purchasing data.

22
00:01:25,120 --> 00:01:31,090
And that will basically just print out when the user taps the buy button until they either purchase

23
00:01:31,150 --> 00:01:33,160
restore fail or defer.

24
00:01:33,160 --> 00:01:34,780
So that's being called.

25
00:01:34,780 --> 00:01:41,290
But you know what I don't really like how I'm not sending a notification when I actually buy it because

26
00:01:41,290 --> 00:01:48,100
that that implies that our function here is going to get called every single time and we want to make

27
00:01:48,100 --> 00:01:49,230
sure that it gets called.

28
00:01:49,230 --> 00:01:58,000
So let's go ahead and let's actually modify complete to be the way it was before let's call case IAP

29
00:01:58,000 --> 00:01:59,500
mealtime monthly sub.

30
00:01:59,920 --> 00:02:04,870
And then what we're going to do is we're going to actually call the same code from this function right

31
00:02:04,870 --> 00:02:10,190
here for a successful subscription because at that point we have succeeded as well.

32
00:02:10,190 --> 00:02:11,420
Now let's go.

33
00:02:11,530 --> 00:02:13,150
And let's type that.

34
00:02:13,150 --> 00:02:16,590
Here we're going to paste it and then we're actually going to break.

35
00:02:16,840 --> 00:02:17,630
Okay.

36
00:02:18,100 --> 00:02:19,690
So there we go.

37
00:02:19,690 --> 00:02:21,730
Beautiful.

38
00:02:21,730 --> 00:02:23,890
We have an executable statement we're calling true.

39
00:02:23,920 --> 00:02:25,390
We're setting that boolean.

40
00:02:25,390 --> 00:02:27,820
We are also setting the non consumable purchase to be true.

41
00:02:27,820 --> 00:02:29,330
So go ahead and build and run.

42
00:02:29,320 --> 00:02:32,520
Let's go see if this helps to fix our problem.

43
00:02:32,620 --> 00:02:35,680
Our subscription should be expired from the last video.

44
00:02:36,010 --> 00:02:38,420
And let's go ahead and check this out.

45
00:02:38,440 --> 00:02:44,080
It says subscription active and when we loaded up the app look at that it actually bought a new subscription

46
00:02:44,080 --> 00:02:44,830
for us.

47
00:02:44,950 --> 00:02:46,810
So that is not a good thing.

48
00:02:46,810 --> 00:02:50,280
That is not doing what it's supposed to be doing very uncool.

49
00:02:50,350 --> 00:02:52,840
So we need to fix this.

50
00:02:52,840 --> 00:02:59,990
Let's go ahead and let's go into storefront Visi let's think about what we could be missing here.

51
00:03:00,070 --> 00:03:02,150
What are we missing.

52
00:03:03,820 --> 00:03:07,720
Oh we are missing a very important thing.

53
00:03:07,750 --> 00:03:12,550
We are adding observers for these notifications but we are never removing them.

54
00:03:12,550 --> 00:03:13,750
That is a huge problem.

55
00:03:13,750 --> 00:03:16,130
I am surprised we have not noticed that until right now.

56
00:03:16,420 --> 00:03:18,050
Okay so you don't we're actually going to do.

57
00:03:18,060 --> 00:03:23,200
We're going to call and net and whenever this view controller is DNA sized we're going to just remove

58
00:03:23,230 --> 00:03:24,970
all the observers on this view controller.

59
00:03:24,970 --> 00:03:31,520
So go ahead and call notification center dot default dot remove observer and call self.

60
00:03:31,550 --> 00:03:32,830
OK.

61
00:03:32,920 --> 00:03:37,440
So now whenever this view controller is d initialized it's going to remove that observer.

62
00:03:37,540 --> 00:03:42,520
And that way we won't accidentally get you know observers that are just kind of locked in memory for

63
00:03:42,520 --> 00:03:47,010
when the app opens again so called DNS and remove the observers here guys.

64
00:03:47,040 --> 00:03:49,400
This is really important I'm surprised we didn't do this.

65
00:03:49,420 --> 00:03:56,890
We also need to call this on detail Visi like so let's go ahead and put it right beneath you will disappear

66
00:03:57,460 --> 00:03:58,430
like so.

67
00:03:58,600 --> 00:04:03,610
And oh you know what it looks like we are removing it from from view will disappear.

68
00:04:03,610 --> 00:04:07,570
Let's actually we don't need to call view will disappear it will just do initialize it because whenever

69
00:04:07,570 --> 00:04:10,430
the view controller goes away it's initialized by Swift.

70
00:04:10,630 --> 00:04:12,530
So we can put that there.

71
00:04:12,850 --> 00:04:14,280
Let's go ahead and build and run.

72
00:04:14,470 --> 00:04:16,860
OK let's go ahead and check out what we did here.

73
00:04:17,080 --> 00:04:20,190
And let's see if this helps.

74
00:04:22,520 --> 00:04:22,810
OK.

75
00:04:22,820 --> 00:04:23,820
Subscription is active.

76
00:04:23,840 --> 00:04:26,440
That's true because we still have about two minutes left.

77
00:04:26,600 --> 00:04:28,160
Being 841.

78
00:04:28,400 --> 00:04:34,270
So we're going to just go ahead and hang tight while the subscription is still active.

79
00:04:34,340 --> 00:04:39,140
It does appear that it is sending through OK I can go into my different view controllers.

80
00:04:39,140 --> 00:04:40,230
The ads are gone.

81
00:04:40,320 --> 00:04:42,570
Ok that's cool.

82
00:04:42,740 --> 00:04:49,550
So yeah I think that this will help removing the observers from storefront Visi because that is actually

83
00:04:49,880 --> 00:04:54,620
that might be the source of our issue here is that the notifications are never being removed and they

84
00:04:54,620 --> 00:05:00,080
might be getting stuck in memory which could be a part of our problem here so let's just verify that

85
00:05:00,080 --> 00:05:03,290
everything is good if the status comes in as true.

86
00:05:03,290 --> 00:05:04,670
We're updating the label.

87
00:05:04,670 --> 00:05:08,940
We're going to update the the label to read if it's not true.

88
00:05:09,230 --> 00:05:09,970
You don't want.

89
00:05:10,030 --> 00:05:11,630
Why don't we do some more fun stuff.

90
00:05:11,630 --> 00:05:15,210
Why don't we say something like self-direct.

91
00:05:15,590 --> 00:05:19,420
Well and that self may be viewed background color.

92
00:05:19,730 --> 00:05:24,150
Let's change the background color when we are subscribed let's do like a dark mode kind of thing.

93
00:05:24,200 --> 00:05:27,510
So let's go ahead and set it to be like a dark gray.

94
00:05:27,540 --> 00:05:28,890
That looks pretty cool.

95
00:05:28,910 --> 00:05:31,230
And why don't we set the collection view.

96
00:05:31,400 --> 00:05:37,890
Background color to also be that dark gray using a color literal cool.

97
00:05:38,440 --> 00:05:39,380
Subscription is active.

98
00:05:39,390 --> 00:05:40,720
Ok that's cool.

99
00:05:40,720 --> 00:05:43,280
We're going to we've got to use self since we're in a closure.

100
00:05:43,310 --> 00:05:48,110
Do it again self and copy this paste it below.

101
00:05:48,140 --> 00:05:53,450
And we're going to set this back to white K when we're expired we're going to have normal light mode

102
00:05:53,480 --> 00:05:56,220
and then when we are subscribed we'll have a dark mode.

103
00:05:56,300 --> 00:06:00,610
Let's build and run just to see that while we're waiting for a subscription to expire and you know what

104
00:06:00,620 --> 00:06:02,630
it actually may already be expired.

105
00:06:02,720 --> 00:06:03,900
So that's fine.

106
00:06:04,930 --> 00:06:05,400
Okay.

107
00:06:05,510 --> 00:06:06,270
It's expired.

108
00:06:06,320 --> 00:06:07,340
The background is white.

109
00:06:07,340 --> 00:06:08,070
Very cool.

110
00:06:08,090 --> 00:06:11,900
Let's tap on purchase and let's enter our password.

111
00:06:11,930 --> 00:06:16,550
Test test one two three is thing on press OK.

112
00:06:17,030 --> 00:06:18,230
And let's see.

113
00:06:18,230 --> 00:06:18,860
Here we go.

114
00:06:18,890 --> 00:06:28,730
Confirm purchase confirms subscription and we're waiting to see if it actually gets confirmed.

115
00:06:31,450 --> 00:06:32,120
Boom.

116
00:06:32,260 --> 00:06:34,480
Oh my gosh do you see that subscription is active.

117
00:06:34,480 --> 00:06:36,620
That is the coolest thing.

118
00:06:36,670 --> 00:06:37,810
It worked.

119
00:06:37,810 --> 00:06:38,700
Look at that.

120
00:06:39,040 --> 00:06:42,970
And now that our subscription is active we can actually go in and we can view this.

121
00:06:42,970 --> 00:06:44,560
Everything is good to go.

122
00:06:44,560 --> 00:06:48,300
We are not having issues with our subscription anymore.

123
00:06:48,400 --> 00:06:49,600
If we build and run.

124
00:06:49,600 --> 00:06:51,670
Let's go back and let's take a look.

125
00:06:51,670 --> 00:06:57,190
We should still have our dark mode as soon as it loads updating subscription active.

126
00:06:57,200 --> 00:06:59,650
Wow that is so cool.

127
00:06:59,650 --> 00:07:01,890
I do not like the food Zyla is still gray.

128
00:07:01,900 --> 00:07:03,300
So we're going to fix that really quickly.

129
00:07:03,310 --> 00:07:07,810
But guys we have a subscription when it expires.

130
00:07:07,810 --> 00:07:13,630
We can actually see how long we have we can know when it expires when we reload the app that subscription

131
00:07:13,900 --> 00:07:20,220
is going to go ahead and show as expired and it will turn the user interface back to white.

132
00:07:20,260 --> 00:07:25,090
Our ads are there earn our ads are not there as part of our subscription.

133
00:07:25,090 --> 00:07:27,780
Wow guys this is really really cool.

134
00:07:27,880 --> 00:07:33,540
So let's go ahead and head into main storyboard and let's set up our food Zyla label to turn white.

135
00:07:33,550 --> 00:07:41,290
When we want it to let's just right click and drag from that label and we'll just say food Zilah label

136
00:07:42,280 --> 00:07:46,130
go back into the store front and in our handling function.

137
00:07:46,270 --> 00:07:47,460
Go ahead and call.

138
00:07:47,670 --> 00:07:49,330
Let's just say self.

139
00:07:49,510 --> 00:07:55,840
Food Zyla label dot text color and we're going to set it to be a color literal of white when the background

140
00:07:55,840 --> 00:08:02,480
is dark and then we're going to go ahead and change it back to the same dark gray color.

141
00:08:02,800 --> 00:08:03,000
Yeah.

142
00:08:03,010 --> 00:08:06,970
This one here when the subscriptions expired so let's build and run.

143
00:08:06,970 --> 00:08:09,890
Let's see how our dark mode looks.

144
00:08:10,510 --> 00:08:11,500
So cool guys.

145
00:08:11,530 --> 00:08:13,140
Awesome awesome awesome.

146
00:08:13,150 --> 00:08:16,270
So a building run here let's see.

147
00:08:16,270 --> 00:08:22,100
Subscription is active and food Zyla turns white that looks so cool.

148
00:08:22,310 --> 00:08:23,920
And guys this is amazing.

149
00:08:23,920 --> 00:08:25,660
Our subscription is working.

150
00:08:25,660 --> 00:08:30,910
I do want to add one more thing that will allow us basically just for testing purposes.

151
00:08:30,910 --> 00:08:36,400
I found that it was really really helpful to have a print out that showed how many minutes I had left

152
00:08:36,880 --> 00:08:38,500
it was just helpful to see.

153
00:08:38,590 --> 00:08:44,890
And so what I'm actually going to do is inside our function where we check if it's active which is in

154
00:08:44,890 --> 00:08:47,720
our IP service way up here.

155
00:08:48,130 --> 00:08:54,190
What we're going to do is we're actually going to go ahead and print out the expiration date and then

156
00:08:54,190 --> 00:08:59,260
we're going to use something called time interval since now which basically gives us the amount of seconds

157
00:09:00,310 --> 00:09:04,650
until the expiration date and then we're just going to divide it by 60 to get the amount of minutes.

158
00:09:04,660 --> 00:09:13,820
So go ahead and call debug print and then we're going to actually print out time remaining time remaining

159
00:09:14,830 --> 00:09:21,080
and then we can go ahead and pass in the expiration date Dot time interval since now which says the

160
00:09:21,080 --> 00:09:24,400
time interval between the date value and the current date and time.

161
00:09:24,500 --> 00:09:29,210
So it tells us how long we have until this subscription and then basically we're just going to divide

162
00:09:29,210 --> 00:09:33,950
that by 60 and print it out and that'll tell us how many minutes we have.

163
00:09:33,950 --> 00:09:37,590
Now it's saying value of optional type not unwrapped.

164
00:09:37,940 --> 00:09:38,760
And that's true.

165
00:09:38,780 --> 00:09:41,890
So we need to think about how we're going to handle this.

166
00:09:42,200 --> 00:09:47,120
You know what at this point we have reloaded the expiration date so we know it's going to be good.

167
00:09:47,180 --> 00:09:51,760
So we can actually go ahead and put this in parentheses and force on rapid like so.

168
00:09:52,130 --> 00:09:55,240
So yeah build and run that and the arrow will go away.

169
00:09:55,250 --> 00:10:00,110
We do know we will have a date and we can you know print out the time remaining so.

170
00:10:00,140 --> 00:10:01,900
Now let's check it out.

171
00:10:01,910 --> 00:10:03,260
Awesome time remaining.

172
00:10:03,260 --> 00:10:13,100
And well let's add minutes to the end of that and it's like so and it'll say X minutes.

173
00:10:13,100 --> 00:10:15,680
OK so that's pretty cool 1.3 minutes.

174
00:10:15,830 --> 00:10:23,100
Let's just double check to make sure that's accurate 1.0 minutes it expires at 4:48 and 49 seconds that

175
00:10:23,210 --> 00:10:24,740
that could be about a minute.

176
00:10:24,980 --> 00:10:27,740
Let's say it should turn to 8:48 soon.

177
00:10:28,180 --> 00:10:35,850
And yeah that's cool that's going to let us check how many minutes we have BuildOn run.

178
00:10:35,900 --> 00:10:37,390
Very cool.

179
00:10:37,810 --> 00:10:40,160
And yeah point seven three minutes yeah that's about right.

180
00:10:40,160 --> 00:10:45,830
So in point seven three minutes whatever that means we're going to go ahead and verify that our subscription

181
00:10:46,070 --> 00:10:49,060
goes back to non active.

182
00:10:49,140 --> 00:10:54,910
And I just realized you know we could do we can call it subscription active from view will appear so

183
00:10:54,910 --> 00:10:59,840
that if we were to close out while this description expires Oh you know what you should see that if

184
00:10:59,840 --> 00:11:05,660
we were to open this and then close out like so we can call is subscription active and we could you

185
00:11:05,660 --> 00:11:07,070
know act accordingly.

186
00:11:07,070 --> 00:11:15,910
So let's go ahead and head into storefront Visi Lets call view will appear called Super view will appear

187
00:11:17,390 --> 00:11:25,640
and passen animated then we can go ahead and call IAP service instance dot is subscription active and

188
00:11:25,640 --> 00:11:30,850
for the completion handler we can just stay active and we dont actually have to do anything in that.

189
00:11:30,850 --> 00:11:34,670
We just want to call it now we could make the completion händler optional.

190
00:11:34,820 --> 00:11:36,490
I'm just not going to.

191
00:11:36,500 --> 00:11:41,080
So let's go ahead let's build and run let's just verify that our subscription is good to go.

192
00:11:41,090 --> 00:11:47,300
And actually I think it's expired at the moment so we should see inactive and yes as you can see the

193
00:11:47,300 --> 00:11:48,890
subscription has expired.

194
00:11:48,890 --> 00:11:53,320
If I open up a screen you can see the ads are back but watch what happens when I close it.

195
00:11:53,320 --> 00:11:59,120
Now when the view appears it tells me how many minutes are remaining and that means my subscription

196
00:11:59,120 --> 00:12:00,340
is not active.

197
00:12:00,500 --> 00:12:08,600
So what I can do is I can go ahead and resubscribe tap confirm purchase confirm subscription and my

198
00:12:08,600 --> 00:12:09,880
subscription is active.

199
00:12:09,890 --> 00:12:12,190
Now if I open it the ads are gone.

200
00:12:12,230 --> 00:12:14,720
If I close it it gives me the time remaining.

201
00:12:14,720 --> 00:12:16,790
So yeah guys our subscription is valid.

202
00:12:16,790 --> 00:12:22,250
We have about four minutes left and this is this is it guys this is how you do auto renewing subscriptions

203
00:12:22,260 --> 00:12:26,540
and AOS we're able to subscribe when it expires.

204
00:12:26,540 --> 00:12:33,260
The user interface changes the feature the features change and we're able to pretty much control whatever

205
00:12:33,260 --> 00:12:36,840
we want using our IAP sub info has changed notification.

206
00:12:36,860 --> 00:12:38,190
Very very cool.

207
00:12:38,210 --> 00:12:43,070
There is our subscription interface again and our ads that are gone which is cool.

208
00:12:43,100 --> 00:12:48,560
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

209
00:12:48,560 --> 00:12:49,940
to make this app even better.

210
00:12:50,000 --> 00:12:52,440
So let's head over there now.
