1
00:00:07,120 --> 00:00:11,740
Hey everybody what's going on this is Caleb with slopes dot com and in this video we're going to set

2
00:00:11,740 --> 00:00:18,460
up restoring our purchases that we already bought which is really cool it's a handy feature even if

3
00:00:18,730 --> 00:00:23,400
you know you don't have it and someone wants to buy you're in that purchase that's non consumable.

4
00:00:23,560 --> 00:00:27,820
The apps are automatically going to say hey you've already bought this habit for free but it's helpful

5
00:00:27,820 --> 00:00:32,020
to have restore purchases in the instance of an app where you have maybe like 30 different in-app purchases

6
00:00:32,020 --> 00:00:33,580
and maybe they've bought seven of them.

7
00:00:33,580 --> 00:00:36,360
They don't have to go through and individually buy them all.

8
00:00:36,360 --> 00:00:42,120
You can set it up this way so that they could you know restore them all at once which is really neat.

9
00:00:42,130 --> 00:00:47,630
So you pull open your X code project and we're going to go into the IAP service.

10
00:00:47,710 --> 00:00:53,520
We're going to go ahead and close this out and we're going to basically set up our function for one

11
00:00:53,530 --> 00:00:54,970
that's going to restore purchases.

12
00:00:55,000 --> 00:00:56,650
Super super easy guys.

13
00:00:56,860 --> 00:01:01,630
And then we're going to set up some of the other things we need to like the payment Q for our restored

14
00:01:01,630 --> 00:01:07,990
case we're also going to set up a function here called restored and that restored function is going

15
00:01:07,990 --> 00:01:09,430
to send a notification.

16
00:01:09,430 --> 00:01:12,820
It's also going to set the value for our user defaults.

17
00:01:12,820 --> 00:01:15,440
So let's go ahead and let's dive right in.

18
00:01:15,460 --> 00:01:20,020
The function is unbelievably easy and we're going to go ahead and put that down here.

19
00:01:20,320 --> 00:01:29,380
Going to say phunk Let's move that more in the middle phunk restore purchases like that and then we're

20
00:01:29,380 --> 00:01:30,820
going to call S-K payment.

21
00:01:30,830 --> 00:01:39,140
Q default and then datt restore restore completed transactions.

22
00:01:39,150 --> 00:01:39,900
That's it.

23
00:01:39,940 --> 00:01:41,910
Seriously that's all we need to do.

24
00:01:42,040 --> 00:01:45,280
And when we call this it's going to go ahead and call us through the payment.

25
00:01:45,280 --> 00:01:51,790
Q So it restored is a little bit different because we wrote comp. here because we needed to do specific

26
00:01:51,790 --> 00:01:57,550
things after we complete but restored is going to do something a little bit different it already has

27
00:01:57,550 --> 00:01:58,600
its own delegate method.

28
00:01:58,600 --> 00:02:04,660
That's a part of S-K payment transaction observer that we can utilize that gets called after a Restore

29
00:02:04,660 --> 00:02:05,740
is successfully finished.

30
00:02:05,740 --> 00:02:12,910
So go ahead and just call restore and right here look at this payment to restore completed transactions

31
00:02:12,910 --> 00:02:17,820
finished push enter and this is called whenever a restore successfully happens.

32
00:02:17,830 --> 00:02:23,950
And so what we're going to do is we're going to send a notification for status check it out restored

33
00:02:24,460 --> 00:02:30,430
with identifier of nil because we don't need the identifier for this because it's just going to restore

34
00:02:30,430 --> 00:02:32,110
everything we've already purchased.

35
00:02:32,410 --> 00:02:38,680
And then if it's restored remember the purchase is hide adds that's the non consumable one that gets

36
00:02:38,680 --> 00:02:39,560
restored.

37
00:02:39,580 --> 00:02:44,170
So therefore we need to go ahead and call set non consumable purchase to be true.

38
00:02:44,170 --> 00:02:45,050
All right.

39
00:02:45,400 --> 00:02:50,140
And guys I'm not kidding that's about all we need to do here.

40
00:02:50,140 --> 00:02:51,900
One one thing sorry that I did forget.

41
00:02:51,910 --> 00:02:53,650
We do need to call S-K payment.

42
00:02:53,650 --> 00:02:58,070
Q finished transaction because it is a transaction nonetheless.

43
00:02:58,090 --> 00:03:01,030
But then it's going to go ahead and call this delegate method.

44
00:03:01,030 --> 00:03:06,430
Send the notification wherever we need it and then set the non consumable purchases to be true.

45
00:03:06,460 --> 00:03:09,310
Now at the moment we're not sending a notification anywhere.

46
00:03:09,310 --> 00:03:14,590
So let's go ahead and head into storefront Visi where our restore button is.

47
00:03:14,590 --> 00:03:17,460
And let's set that up first actually.

48
00:03:17,500 --> 00:03:21,760
So restore button if you remember is the question mark button in the top right hand side of the screen

49
00:03:22,100 --> 00:03:25,600
show you on my little simulator here.

50
00:03:25,600 --> 00:03:27,830
This button is the restore button.

51
00:03:28,240 --> 00:03:32,470
And in order to actually use that we're going to go ahead and present an alert Visi whenever that button

52
00:03:32,470 --> 00:03:33,070
is pressed.

53
00:03:33,070 --> 00:03:35,290
Giving them an option do you want to restore or not.

54
00:03:35,290 --> 00:03:36,950
So they have a choice.

55
00:03:37,120 --> 00:03:38,250
So let's go ahead and do that.

56
00:03:38,530 --> 00:03:42,610
Let alert Visi equals UI alert controller.

57
00:03:42,660 --> 00:03:46,960
Go ahead and instantiate it and we're going to choose the one with a title message and preferred style

58
00:03:47,500 --> 00:03:53,410
the preferred style or sorry the title is going to say restore purchases.

59
00:03:53,410 --> 00:03:54,460
With a question mark.

60
00:03:54,730 --> 00:04:06,040
The message is going to say do you want to restore any in App purchases you've previously bought maybe

61
00:04:06,040 --> 00:04:08,130
you previously bought.

62
00:04:08,530 --> 00:04:11,410
Or maybe you've previously purchased.

63
00:04:11,410 --> 00:04:16,570
That sounds a little more professional and the style normally you would do alert which is like a pop

64
00:04:16,570 --> 00:04:18,490
up alert but I really like action sheets.

65
00:04:18,490 --> 00:04:19,490
I think they're pretty cool.

66
00:04:19,720 --> 00:04:21,710
So I'm going to use action instead.

67
00:04:21,790 --> 00:04:27,010
And now we're going to actually create the action that happens whenever the button is pressed.

68
00:04:27,040 --> 00:04:32,120
So go ahead and type let action equals UI alert action.

69
00:04:32,170 --> 00:04:37,290
Now go ahead and instantiate that as well with a title style and handler and the title.

70
00:04:37,360 --> 00:04:39,460
This is what the button is actually going to say.

71
00:04:39,610 --> 00:04:44,800
We're going to say restore the style is going to be default and for the handler.

72
00:04:44,800 --> 00:04:46,720
Go ahead and select it press enter.

73
00:04:46,720 --> 00:04:48,480
And we're just going to say Action.

74
00:04:48,600 --> 00:04:48,830
OK.

75
00:04:48,850 --> 00:04:50,910
We don't really need what comes back from that.

76
00:04:50,920 --> 00:04:56,800
But inside this closure we can perform certain behaviors and so the one we want to perform is from our

77
00:04:57,250 --> 00:05:04,270
pre-service instance that restore purchases and that's all we need to do to restore our purchases successfully.

78
00:05:04,270 --> 00:05:05,710
So that's one thing.

79
00:05:05,780 --> 00:05:13,160
Let's go ahead and create a cancel action as well so we can cancel and just do that by typing let cancell

80
00:05:14,440 --> 00:05:18,510
action equals you alert action.

81
00:05:18,500 --> 00:05:20,040
Whoops.

82
00:05:20,200 --> 00:05:22,580
The title is going to be cancel.

83
00:05:22,750 --> 00:05:28,210
The style is going to be cancel and the alert handler is going to be nil.

84
00:05:28,240 --> 00:05:29,170
You'll notice it's optional.

85
00:05:29,170 --> 00:05:30,110
You don't have to use it.

86
00:05:30,110 --> 00:05:32,600
And that makes it perfect for canceling.

87
00:05:32,620 --> 00:05:39,520
Next we need to add our actions to our alert Visi so call alert Visi add action.

88
00:05:39,700 --> 00:05:46,360
Add the first action like so then call it again alert VCM at action you can add an array of actions

89
00:05:46,360 --> 00:05:49,670
which is kind of annoying but anyway cancel action.

90
00:05:49,930 --> 00:05:52,030
Then at the very end we need to present

91
00:05:55,390 --> 00:05:57,670
are alert you see animated.

92
00:05:57,680 --> 00:06:00,650
True and completion is nil.

93
00:06:00,670 --> 00:06:05,650
Now when we push our button it's going to present this alert Visi And if we push on the restore button

94
00:06:06,520 --> 00:06:10,780
we're going to be able to call our restore function which if you remember let's go into it let's go

95
00:06:10,780 --> 00:06:11,990
find it.

96
00:06:12,010 --> 00:06:14,670
It's going to call restore completed transactions.

97
00:06:14,830 --> 00:06:17,020
It's going to run through the payment queue.

98
00:06:17,170 --> 00:06:22,390
It will finish the transaction and then call payment restore completed transactions which is going to

99
00:06:22,390 --> 00:06:28,450
send our restored notification and set the non consumable purchase to be true which is really cool because

100
00:06:28,450 --> 00:06:34,930
it's going to let us know set our view to be hidden by the time that this even loads when it's restored

101
00:06:35,140 --> 00:06:40,180
it sets that boolean value which then in detail Visi is set here.

102
00:06:40,180 --> 00:06:43,940
So when we present the view it will show up as hidden which is really neat.

103
00:06:43,990 --> 00:06:50,860
Now we do need to maybe present a an alert Visi that says Hey thanks for that your purchases are restored.

104
00:06:51,160 --> 00:06:59,380
And so to do that we're going to go ahead and set up an observer for that IAP service restore notification

105
00:06:59,830 --> 00:07:03,780
and then whenever that is notified we're going to present an alert visi.

106
00:07:03,850 --> 00:07:11,770
So go ahead and type notification center notification type notification center that default dot our

107
00:07:11,860 --> 00:07:18,640
observer and we're going to say the observer is self the selector will get back to the notification

108
00:07:18,670 --> 00:07:25,450
is notification name and for the name pasan IAP service restore notification.

109
00:07:25,450 --> 00:07:31,030
The object can be nil and the selector we're going to make a function that presents an alert Visi saying

110
00:07:31,060 --> 00:07:33,020
hey your purchases were restored.

111
00:07:33,100 --> 00:07:35,990
So let's do that down here.

112
00:07:36,220 --> 00:07:37,420
Let's go ahead and just.

113
00:07:37,630 --> 00:07:39,280
Well maybe not below the actions.

114
00:07:39,280 --> 00:07:40,810
Let's do it right below.

115
00:07:40,810 --> 00:07:43,110
Are you a collection of your stuff.

116
00:07:43,180 --> 00:07:51,480
Go ahead and type at obedience at O.B. J.C. Funk show alert.

117
00:07:52,180 --> 00:07:56,770
And we're going to say let alert Visi we're kind of doing the same thing we probably should put this

118
00:07:56,770 --> 00:08:00,360
in an extension but it's ok we can just do it this way.

119
00:08:00,460 --> 00:08:10,790
The title will say success and the message will say your purchases were successfully restored.

120
00:08:12,720 --> 00:08:15,450
The style is going to be an alert just to pop up.

121
00:08:15,450 --> 00:08:18,000
Let's go ahead and let's add an action.

122
00:08:18,030 --> 00:08:20,330
Action equals UI alert action.

123
00:08:20,610 --> 00:08:23,770
And we're going to instantiate that with a title the button.

124
00:08:23,820 --> 00:08:24,750
Right that's the action.

125
00:08:24,750 --> 00:08:30,660
Just going to say OK the style is going to be default and the handler is going to be nil we don't care

126
00:08:30,660 --> 00:08:31,110
what happens.

127
00:08:31,110 --> 00:08:32,590
We just wanted to go away.

128
00:08:32,790 --> 00:08:41,750
Now add alert VCM add action and add the action then present alert b.c.

129
00:08:42,070 --> 00:08:42,580
And you know what.

130
00:08:42,580 --> 00:08:44,160
This is not really a view controller.

131
00:08:44,200 --> 00:08:46,860
I mean it is sort of but let's just call it an alert.

132
00:08:46,990 --> 00:08:49,770
Makes a little more sense.

133
00:08:49,870 --> 00:08:50,650
I think that's good.

134
00:08:50,650 --> 00:08:52,440
Same with this it's not really a controller.

135
00:08:52,510 --> 00:08:55,100
It's just an alert that pops up over the view controller.

136
00:08:55,570 --> 00:08:58,120
So we'll just call it an alert and we'll present dealer.

137
00:08:58,120 --> 00:09:03,270
I mean technically speaking if you dive in deep you can see that and it inherits from you and you controller

138
00:09:03,310 --> 00:09:07,050
but I don't like calling it if you control or even is.

139
00:09:07,060 --> 00:09:10,330
So now when we push our restore button we should restore purchases.

140
00:09:10,330 --> 00:09:16,930
If successful we should be able to you know show an alert which is neat and we're just going to go ahead

141
00:09:16,960 --> 00:09:25,540
and I guess we can call our selector here poun selector and let's actually rename this function let's

142
00:09:25,540 --> 00:09:30,220
just say show restored alert and we can call that here.

143
00:09:30,610 --> 00:09:35,250
So when we successfully restore it will pop up and say hey you restored.

144
00:09:35,530 --> 00:09:36,470
Let's go try it.

145
00:09:36,470 --> 00:09:37,070
And you know what.

146
00:09:37,090 --> 00:09:37,930
Oh sorry guys.

147
00:09:37,930 --> 00:09:40,460
Before we build it I'm going to cancel this.

148
00:09:40,480 --> 00:09:45,130
I do need to delete it from my phone because I've already bought the in-app purchase meaning that the

149
00:09:45,130 --> 00:09:48,460
ads will be hidden and there's no way for us to tell if the restore worked.

150
00:09:48,460 --> 00:09:49,740
So I just deleted it.

151
00:09:49,810 --> 00:09:51,040
I'm installing it now.

152
00:09:51,070 --> 00:09:54,370
And as soon as it pops up I will show you in quick time.

153
00:09:54,370 --> 00:09:54,880
Here we go.

154
00:09:54,880 --> 00:09:55,140
OK.

155
00:09:55,150 --> 00:09:56,540
So let's take a look.

156
00:09:56,560 --> 00:09:57,450
The ads are there.

157
00:09:57,560 --> 00:09:58,930
OK that's good.

158
00:09:58,930 --> 00:10:03,870
Now when I tap this it should show our pop up there we go.

159
00:10:03,870 --> 00:10:11,050
Do you want to restore an in-app purchases tap restore your purchases were successfully restored.

160
00:10:11,050 --> 00:10:13,450
Press OK and let's go check.

161
00:10:14,020 --> 00:10:14,690
Oh my gosh.

162
00:10:14,710 --> 00:10:19,780
That's amazing because I've already purchased the hide ads and at purses it shows up right there.

163
00:10:19,780 --> 00:10:20,310
Boom.

164
00:10:20,320 --> 00:10:21,870
Oh my gosh this is amazing guys.

165
00:10:22,030 --> 00:10:23,630
So this is this is done.

166
00:10:23,650 --> 00:10:30,520
We have now properly set up consumables and non consumables in the same app with iTunes Connect with

167
00:10:30,520 --> 00:10:31,270
everything we need.

168
00:10:31,270 --> 00:10:36,010
We have an awesome IP service that processes all of it does all the cool stuff.

169
00:10:36,010 --> 00:10:37,570
Oh my goodness guys.

170
00:10:37,570 --> 00:10:38,450
This is incredible.

171
00:10:38,470 --> 00:10:40,650
Awesome awesome work and congratulations.

172
00:10:40,650 --> 00:10:46,410
You now have a great foundation in in-app purchases consumable and non consumables.

173
00:10:46,420 --> 00:10:52,210
Stay tuned for the next target topic which is going to be all about auto renewing subscriptions as a

174
00:10:52,210 --> 00:10:53,580
form of in-app purchases.

175
00:10:53,590 --> 00:10:54,660
Very very cool.

176
00:10:54,670 --> 00:10:59,680
We're going to do all kinds of amazing stuff like a receipt validation to prevent yourself against jail

177
00:10:59,680 --> 00:11:04,200
broken phones who have one of those in app purchased tweaks that let's let them kind of hack them onto

178
00:11:04,210 --> 00:11:04,950
their phone.

179
00:11:04,960 --> 00:11:09,070
We're going to be able to actually upload the receipt that's generated and verify it against Apple's

180
00:11:09,070 --> 00:11:10,030
servers.

181
00:11:10,030 --> 00:11:11,290
Otherwise it's not going to work.

182
00:11:11,290 --> 00:11:16,200
Super cool stuff to help you as a business person who's developing an app.

183
00:11:16,270 --> 00:11:17,140
This is your money maker.

184
00:11:17,140 --> 00:11:19,060
You can't have people stealing your in-app purchases.

185
00:11:19,060 --> 00:11:20,890
So we'll make sure it doesn't happen.

186
00:11:20,890 --> 00:11:22,020
Amazing work guys.

187
00:11:22,030 --> 00:11:24,230
Thank you so much for watching this course.

188
00:11:24,370 --> 00:11:28,690
And I just wish you the most success in integrating in-app purchases into your app.

189
00:11:28,690 --> 00:11:32,090
This is Caleb with Debb slopes dot com and I'll see in the next target topic.
