1
00:00:07,150 --> 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 get

2
00:00:11,740 --> 00:00:12,670
into the code.

3
00:00:12,670 --> 00:00:18,430
We're going to start basically by requesting to purchase our subscription that we just made in iTunes

4
00:00:18,430 --> 00:00:18,850
Connect.

5
00:00:18,850 --> 00:00:23,610
So go ahead and pull open your X code project that you have been working in.

6
00:00:24,010 --> 00:00:29,740
And first we're going to dive into the IAP service and I want to basically just show you everything

7
00:00:29,740 --> 00:00:31,450
that's in there and explain what it's doing.

8
00:00:31,450 --> 00:00:38,380
So select IAP service and basically what's going on and I already touched on this a bit but when the

9
00:00:38,380 --> 00:00:45,430
app loads it's loading all of our IP products from iTunes and you can follow the other course if you

10
00:00:45,430 --> 00:00:46,880
want to see how this is done.

11
00:00:47,200 --> 00:00:54,690
But basically we have a function that we can use to attempt a purchase with a certain product index.

12
00:00:54,730 --> 00:00:55,320
Right.

13
00:00:55,660 --> 00:01:02,850
And when we set up this project we created some constants and these are my two other in-app purchases.

14
00:01:02,860 --> 00:01:09,400
But we need to create a constant that's going to point to our in-app purchases from iTunes Connect.

15
00:01:09,490 --> 00:01:15,160
And if you remember that that ID is Comcast IP course meal time monthly.

16
00:01:15,170 --> 00:01:18,940
So I'm actually going to copy that and then create a new constant here.

17
00:01:18,940 --> 00:01:21,550
So go ahead and type let IAP

18
00:01:24,340 --> 00:01:34,720
mealtime monthly sub and set that to be equal to conduct IAP course meal time monthly.

19
00:01:34,720 --> 00:01:37,250
Of course as a string like so.

20
00:01:37,510 --> 00:01:43,220
And now this constant is what we're going to use as an identifier to load our meal time monthly subscription.

21
00:01:43,450 --> 00:01:50,440
So go ahead and go into your IAP service and this is where we basically take all those product IDs and

22
00:01:50,440 --> 00:01:54,090
we convert them into a set of type string.

23
00:01:54,340 --> 00:01:58,230
And then we pass that into our function to request the products from iTunes Connect.

24
00:01:58,230 --> 00:02:04,030
So we're going to actually go ahead and add that ID into this array of IDS just to begin with.

25
00:02:04,030 --> 00:02:12,970
So go ahead and put a comma and type IAP mealtime monthly sub and now it's going to add that as a product

26
00:02:12,970 --> 00:02:13,660
ID.

27
00:02:13,820 --> 00:02:15,690
Okay that's pretty cool.

28
00:02:15,700 --> 00:02:22,800
So now that we have that as a product ID it's going to go ahead and get passed through when we call

29
00:02:22,810 --> 00:02:24,600
product request to start.

30
00:02:24,610 --> 00:02:31,510
Basically what happens is all of these IDs are shot up to iTunes and if they come back as valid products

31
00:02:31,510 --> 00:02:38,260
they get added to our products array here which up at the top you can see is an array of store kit product

32
00:02:38,320 --> 00:02:39,750
Eskay product.

33
00:02:39,910 --> 00:02:47,940
And basically once that array is full we can attempt to purchase for a particular item.

34
00:02:47,950 --> 00:02:48,460
Okay.

35
00:02:48,700 --> 00:02:54,940
Now we can use that products array and we can pull out the index of a certain item so maybe the first

36
00:02:54,940 --> 00:02:56,460
index is hied adds.

37
00:02:56,470 --> 00:03:00,970
The second is meal ID and the third might be meal time monthly sub.

38
00:03:01,390 --> 00:03:06,970
So the way that we have been doing this is we've been using a custom enumeration called product and

39
00:03:06,970 --> 00:03:10,640
I want to show you that product has two cases.

40
00:03:10,660 --> 00:03:17,920
When you download this project one is zero I guess the zero index is hied adds the other one is meal.

41
00:03:17,920 --> 00:03:19,620
We're going to add a third case here.

42
00:03:19,690 --> 00:03:27,360
So go ahead and call or write case monthly sub and we're going to set that to be equal to 2.

43
00:03:27,600 --> 00:03:28,150
OK.

44
00:03:28,360 --> 00:03:32,610
We're simply following the order of the array as we put them in.

45
00:03:32,670 --> 00:03:41,770
Now when we attempt to make a purchase of an item like hide ads we pass in the index of 0 because this

46
00:03:41,770 --> 00:03:44,800
enumeration has an explicit type of int.

47
00:03:44,800 --> 00:03:52,660
Now when we will let me show you when we do that k we go to our detail Visi And let's see what do we

48
00:03:52,660 --> 00:03:53,200
do.

49
00:03:53,260 --> 00:03:55,200
We tap by button ok.

50
00:03:55,390 --> 00:04:01,390
We call IAP service instance and we call that function attempt purchased for item with product index

51
00:04:01,720 --> 00:04:10,090
and we can call meal or hide ads right we pass in that enumeration value which has an explicit value

52
00:04:10,090 --> 00:04:11,970
of 0 1 or 2.

53
00:04:12,190 --> 00:04:18,910
Now in our IP service we pull out that raw value from the index and then use that to access the item

54
00:04:18,910 --> 00:04:26,820
in the array we want K then we pass it into an S-K payment and pass that into the S-K payment queue

55
00:04:26,830 --> 00:04:31,220
which is like a list of transactions that store kit needs to make.

56
00:04:31,210 --> 00:04:38,560
So then after that the payment is made and this delegate method payment queue updated transactions is

57
00:04:38,560 --> 00:04:39,340
called.

58
00:04:39,580 --> 00:04:42,060
That is where we finish the transactions.

59
00:04:42,070 --> 00:04:42,890
OK.

60
00:04:43,300 --> 00:04:46,030
So there are five different things that can happen.

61
00:04:46,270 --> 00:04:53,070
It is either purchased it's restored like I showed you with that blue button it fails.

62
00:04:53,080 --> 00:04:57,750
Payment is deferred meaning payment was not able to be made or purchasing.

63
00:04:57,790 --> 00:04:58,290
OK.

64
00:04:58,510 --> 00:05:02,590
And that's what happens when you tap on the button but you have not yet purchased it loads into the

65
00:05:02,800 --> 00:05:05,350
purchasing temporary time.

66
00:05:05,380 --> 00:05:09,100
I guess there was like a temporary window of time where it's considered to be purchasing.

67
00:05:09,210 --> 00:05:16,230
So let's say that we bought a meal at consumable purchase it comes in here we tap complete and we pass

68
00:05:16,230 --> 00:05:18,470
it into our complete function.

69
00:05:18,480 --> 00:05:19,130
OK.

70
00:05:19,170 --> 00:05:21,530
And that's just directly beneath us.

71
00:05:21,600 --> 00:05:25,130
Now we do different actions depending on what comes in.

72
00:05:25,140 --> 00:05:29,790
If it's the meal we don't really need to do anything because we just finished the transaction because

73
00:05:29,790 --> 00:05:32,090
it's something that you buy over and over again.

74
00:05:32,250 --> 00:05:38,990
But for hide ads we call a function called Set non-consumer purchase and we set it to be true.

75
00:05:39,240 --> 00:05:44,790
Basically what that does is it sets a value in our user defaults to either be true or false depending

76
00:05:44,790 --> 00:05:46,210
on what we pass in.

77
00:05:46,320 --> 00:05:52,710
And the key is non consumable purchase was made because remember if we buy the hide ads in that purchase

78
00:05:52,710 --> 00:05:59,360
We're going to pass in true because we want to basically say hey a non consumable purchase was made

79
00:05:59,670 --> 00:06:05,070
and we want to save that to user defaults so that that purchase is reflected even if we quit the app

80
00:06:05,070 --> 00:06:06,410
and go back in.

81
00:06:06,480 --> 00:06:14,080
So what we need to do is we need to set up a way to purchase our subscription and to do that.

82
00:06:14,190 --> 00:06:19,830
We're going to use this same attempt to purchase function but we need to add a button to our interface.

83
00:06:19,860 --> 00:06:25,230
So let's go ahead and let's head into Maine that story board and we're going to go ahead and select

84
00:06:25,320 --> 00:06:27,910
this button which is a part of a stacked view.

85
00:06:28,230 --> 00:06:34,190
And to duplicate it all we need to do is press command see Command V and now we have a duplicate button

86
00:06:34,230 --> 00:06:40,690
but B where we've already set this button up with an outlet because this button is already connected.

87
00:06:40,710 --> 00:06:45,510
And if we duplicate it the duplicate button is also connected so watch what happens if I right click

88
00:06:46,020 --> 00:06:49,530
it's already connected to another view controller and we do not want that.

89
00:06:49,530 --> 00:06:54,800
So get rid of that connection and we're going to change this to be a dollar sign.

90
00:06:55,150 --> 00:07:01,200
OK press enter to lock it in place and then change the color to a nice green color and this green if

91
00:07:01,200 --> 00:07:04,890
you want to use it is 6 D-B C6 3.

92
00:07:05,070 --> 00:07:05,710
OK.

93
00:07:06,270 --> 00:07:11,880
So now that that is set in place we're going to go ahead and open up a storefront VC and the assistant

94
00:07:11,880 --> 00:07:12,240
editor.

95
00:07:12,240 --> 00:07:15,780
So click on that button and pull this over.

96
00:07:15,780 --> 00:07:22,350
Then we're going to go ahead and scroll all the way down to the bottom and we're going to go ahead and

97
00:07:22,350 --> 00:07:26,310
find the place where this action can go.

98
00:07:26,310 --> 00:07:28,380
We have one for the restore button.

99
00:07:28,440 --> 00:07:32,340
Let's go ahead and let's put this right above the restore button.

100
00:07:32,350 --> 00:07:37,910
So right click and drag from the button and release then go ahead and select action.

101
00:07:37,980 --> 00:07:43,930
And go ahead and type subscribe button was pressed and press connect.

102
00:07:44,040 --> 00:07:48,840
And now we have a function that we can basically do things when we push this button.

103
00:07:48,870 --> 00:07:51,030
Now let's collapse the assistant editor.

104
00:07:51,030 --> 00:07:55,150
Let's go into storefront Visi and find that IAB action again.

105
00:07:55,390 --> 00:07:56,820
OK here it is.

106
00:07:56,820 --> 00:08:01,260
So what we're going to do is we're going to call our attempt to purchase function.

107
00:08:01,260 --> 00:08:08,050
Now go ahead and call that by typing IAP service for instance because remember it's a singleton class.

108
00:08:08,220 --> 00:08:10,070
If you go into here you can see it's a singleton.

109
00:08:10,070 --> 00:08:15,390
We have a static instance that's available to us throughout the whole lifecycle of the application.

110
00:08:15,480 --> 00:08:22,290
Go back and type IAP service for instance dot attempt purchased for item.

111
00:08:22,470 --> 00:08:23,670
Pretty cool.

112
00:08:23,670 --> 00:08:29,610
Now remember this is for subscription so we can go ahead and type dot and use our enumeration to access

113
00:08:29,610 --> 00:08:35,780
the proper index we need which in our cases monthly sub press enter and check it out guys.

114
00:08:35,790 --> 00:08:38,820
That's all we need to do to attempt to purchase.

115
00:08:38,850 --> 00:08:41,100
We should go try this out we should go see how we did.

116
00:08:41,100 --> 00:08:43,210
So go ahead and build and run.

117
00:08:43,260 --> 00:08:50,630
I'm going to pull open my simulator here so you can see what's going on and basically I'm just using

118
00:08:50,630 --> 00:08:56,450
the movie recording feature on quick time which you can actually set your iPhone as a camera and it

119
00:08:56,450 --> 00:08:59,540
lets you view your screen right on your computer over USP.

120
00:08:59,540 --> 00:09:00,510
It's pretty cool.

121
00:09:00,860 --> 00:09:02,820
So let's go ahead.

122
00:09:02,840 --> 00:09:04,450
We're going to build and run that one more time.

123
00:09:04,460 --> 00:09:10,870
Sometimes quick time likes to hijack my you know my session but looks like we're good.

124
00:09:10,910 --> 00:09:11,880
Very cool.

125
00:09:11,930 --> 00:09:14,630
And if you take a look IAP products are loaded.

126
00:09:14,660 --> 00:09:16,760
So that means we've successfully loaded all of them.

127
00:09:16,760 --> 00:09:17,840
There were no errors.

128
00:09:18,020 --> 00:09:21,350
And watch what happens when I try to tap on my subscribe button.

129
00:09:21,350 --> 00:09:22,420
Check this out.

130
00:09:22,580 --> 00:09:25,300
If I push it it's going to ask for a password.

131
00:09:25,430 --> 00:09:29,620
And this is where you're going to have to set up what is called a sandbox tester.

132
00:09:29,750 --> 00:09:30,260
OK.

133
00:09:30,530 --> 00:09:34,700
So what I'm actually going to do is I'm actually going to push cancel on my phone I'm going to go into

134
00:09:34,700 --> 00:09:40,160
my settings and I'm going to go ahead and go to the app store settings and I'm going to show you that

135
00:09:40,160 --> 00:09:41,180
in just a moment.

136
00:09:41,480 --> 00:09:41,820
OK.

137
00:09:41,870 --> 00:09:45,730
So select app store and you'll see I already have an idea logged in.

138
00:09:45,740 --> 00:09:49,540
You'll have your Apple I.D. logged in in the App Store settings.

139
00:09:49,550 --> 00:09:51,500
You're going to go ahead and push sign out.

140
00:09:51,500 --> 00:09:53,840
And don't worry it's only going to sign you out of the App Store.

141
00:09:53,840 --> 00:09:58,430
Not all of your message not your iCloud just just the App Store.

142
00:09:58,490 --> 00:10:01,680
Leave this as it is and then go back to the app.

143
00:10:01,700 --> 00:10:06,640
K now when you select the subscribe button watch what happens.

144
00:10:06,890 --> 00:10:09,780
It's going to ask you to sign into an Apple ID.

145
00:10:09,980 --> 00:10:12,210
This is where iTunes Connect comes into play.

146
00:10:12,440 --> 00:10:14,910
So go ahead and pull open iTunes Connect again.

147
00:10:15,080 --> 00:10:18,630
And what you're going to do is you're going to click on my apps up at the top.

148
00:10:18,920 --> 00:10:23,920
You're going to click users and rolls and then you're going to click on a button that says send box

149
00:10:23,930 --> 00:10:24,940
testers.

150
00:10:25,040 --> 00:10:25,630
OK.

151
00:10:25,910 --> 00:10:31,220
And this is where you're going to basically create a fake Apple ID account that we can use to test our

152
00:10:31,220 --> 00:10:32,200
subscriptions.

153
00:10:32,390 --> 00:10:37,770
So to do that click the plus and we're going to give it a name a last name an email password.

154
00:10:37,760 --> 00:10:40,030
All that stuff just like you would with a real Apple ID.

155
00:10:40,040 --> 00:10:44,600
So I'm just going to say test account and the e-mail.

156
00:10:44,780 --> 00:10:47,860
This is where we're going to use a cool thing called Nahda.

157
00:10:48,030 --> 00:10:48,560
OK.

158
00:10:48,800 --> 00:10:52,320
And in order to do that go to get not a dot com.

159
00:10:52,460 --> 00:10:57,830
And basically it's a temporary disposable e-mail account that you can use when you need it and delete

160
00:10:57,830 --> 00:10:59,080
it when you don't need it anymore.

161
00:10:59,300 --> 00:11:01,080
Which is perfect for test accounts.

162
00:11:01,100 --> 00:11:05,910
So go ahead and click on get me an inbox and check out what happens here.

163
00:11:05,930 --> 00:11:11,210
I already have one but I'm going to create a new one click add more and you can choose how you want

164
00:11:11,210 --> 00:11:12,100
your e-mail to end.

165
00:11:12,120 --> 00:11:14,010
I'm good with a mail doc club.

166
00:11:14,150 --> 00:11:17,660
And we'll just say meal plan.

167
00:11:17,690 --> 00:11:20,930
Aim at our meal plan at a male duck club.

168
00:11:20,930 --> 00:11:21,730
Click Add.

169
00:11:21,830 --> 00:11:22,600
And check it out.

170
00:11:22,610 --> 00:11:23,510
No messages yet.

171
00:11:23,510 --> 00:11:25,930
Your new messages will appear here.

172
00:11:25,980 --> 00:11:29,180
This is my e-mail meal plan at a male duck club.

173
00:11:29,180 --> 00:11:34,100
Click copy and then we can go back to iTunes Connect and paste in that e-mail.

174
00:11:34,130 --> 00:11:38,090
Now you're going to need to remember that and you're also going to need to choose a password for you

175
00:11:38,090 --> 00:11:38,660
to remember.

176
00:11:38,660 --> 00:11:40,640
So go ahead and type a password.

177
00:11:40,640 --> 00:11:45,620
I'm using test test one two three exclamation mark.

178
00:11:45,620 --> 00:11:48,230
Test test one two three exclamation mark.

179
00:11:48,230 --> 00:11:53,940
And my question is just going to be lame What is a test test test.

180
00:11:53,960 --> 00:11:56,210
One two three exclamation mark.

181
00:11:56,210 --> 00:12:00,650
Now choose a date of birth it doesn't really matter and choose your country.

182
00:12:00,680 --> 00:12:03,920
I'm just going to choose United States now when this is done.

183
00:12:03,920 --> 00:12:07,110
Click save when you have all the information filled out.

184
00:12:07,130 --> 00:12:08,180
Remember your password.

185
00:12:08,180 --> 00:12:15,520
Remember the e-mail and click save as soon as that's done it'll show up in your test account.

186
00:12:15,530 --> 00:12:16,040
OK right.

187
00:12:16,040 --> 00:12:18,120
Their meal plan at a male duck club.

188
00:12:18,320 --> 00:12:23,120
And if you go back to nada you'll see you have an e-mail that says verify your Apple ID.

189
00:12:23,150 --> 00:12:28,630
This is very very important if you do not verify your Apple ID.

190
00:12:28,970 --> 00:12:31,260
You are not able to test subscriptions.

191
00:12:31,270 --> 00:12:33,050
And that's why you need to verify.

192
00:12:33,050 --> 00:12:36,710
That's why we're using disposable e-mail so you don't have to create a bunch of Gmail accounts because

193
00:12:36,710 --> 00:12:39,020
that's not not a really cool thing to do.

194
00:12:39,020 --> 00:12:42,350
So we're just using a disposable e-mail because we're not going to need it forever.

195
00:12:42,380 --> 00:12:49,290
So click on that e-mail click verify now just like so and then enter the password you created earlier.

196
00:12:49,310 --> 00:12:53,210
For me it was test test one two three exclamation mark.

197
00:12:53,210 --> 00:12:54,560
Then click continue.

198
00:12:54,710 --> 00:12:58,810
And as soon as that's done it'll say Hey you're good to go green check is good.

199
00:12:58,850 --> 00:13:00,380
E-mail address varified.

200
00:13:00,380 --> 00:13:04,380
So let's go ahead and we're going to head back into our app here.

201
00:13:04,400 --> 00:13:10,100
We're going to click use existing ID and I'm going to go ahead and enter my e-mail which for myself

202
00:13:10,100 --> 00:13:20,470
was meal plan at a male club my password Whoops I spelled that wrong.

203
00:13:20,680 --> 00:13:23,350
A male user like type it right.

204
00:13:23,350 --> 00:13:26,280
Man all right my password was.

205
00:13:26,280 --> 00:13:31,620
Test test one two three exclamation mark and then click.

206
00:13:31,660 --> 00:13:35,040
By now it's going to ask you for it again.

207
00:13:35,110 --> 00:13:37,420
It's going to say hey enter the password.

208
00:13:37,420 --> 00:13:41,350
Use existing Apple ID and sadly you have to do it one more time.

209
00:13:41,710 --> 00:13:42,660
Let's do it again.

210
00:13:42,670 --> 00:13:46,820
Meal Plan this is basically just setting up the account and verifying everything.

211
00:13:47,020 --> 00:13:52,160
A male club.

212
00:13:52,260 --> 00:13:52,580
All right.

213
00:13:52,590 --> 00:13:59,580
And do not try to sign in on in the appstore settings because it actually will not work.

214
00:13:59,580 --> 00:14:06,420
It will register it as an official Apple ID and for this we're actually registering it as a test sandbox

215
00:14:06,420 --> 00:14:08,020
test account.

216
00:14:08,100 --> 00:14:10,300
So test test one two three press.

217
00:14:10,310 --> 00:14:11,830
OK.

218
00:14:12,390 --> 00:14:13,540
And it's not going to do anything.

219
00:14:13,560 --> 00:14:15,540
That's because we have just successfully logged in.

220
00:14:15,540 --> 00:14:21,210
But watch what happens when I push the dollar sign button again boom it asks for the password for that

221
00:14:21,210 --> 00:14:23,790
particular account so now we know we're logged in.

222
00:14:23,790 --> 00:14:28,290
Test test one two three exclamation mark.

223
00:14:28,290 --> 00:14:29,630
And watch what happens when I push.

224
00:14:29,640 --> 00:14:30,450
Ok

225
00:14:33,340 --> 00:14:40,000
look at that subscribe to meal plan get rid of that not now thing subscribe to meal plan this subscription

226
00:14:40,000 --> 00:14:43,530
will automatically renew every month forty 4:49 ninety nine.

227
00:14:43,600 --> 00:14:44,620
That's exactly right.

228
00:14:44,620 --> 00:14:50,470
That's what we just wanted to set up so with if we push that button we're subscribed but it's not going

229
00:14:50,470 --> 00:14:53,880
to be very good for our users because at the moment that doesn't mean a whole lot.

230
00:14:53,880 --> 00:14:56,620
It doesn't do anything so click Cancel.

231
00:14:56,800 --> 00:15:00,810
And this is amazing guys we have our subscription almost completely set up.

232
00:15:00,820 --> 00:15:07,390
We just have to do a couple of things to basically determine how a subscription is going to modify or

233
00:15:07,450 --> 00:15:09,680
influence the way that our app works.

234
00:15:09,700 --> 00:15:13,780
And we're going to do that in a future video so let's go ahead and let's head over to the next video

235
00:15:14,020 --> 00:15:16,740
where we're going to talk about how how do we handle failure.

236
00:15:16,780 --> 00:15:18,700
How do we handle payment deferral.

237
00:15:18,730 --> 00:15:23,830
How do we handle making the official purchase and setting things in user defaults so that they're locked

238
00:15:24,420 --> 00:15:24,930
let's do it.

239
00:15:24,940 --> 00:15:29,130
Let's head over to the next video and let's talk about that stuff right now.
