1
00:00:05,910 --> 00:00:11,410
Hey everyone welcome to the last exciting video in working with in-app purchases.

2
00:00:11,550 --> 00:00:12,360
My name is Mark price.

3
00:00:12,360 --> 00:00:17,760
In case you forgot and we are going to finish this up so what we want to do is first get our restore

4
00:00:17,760 --> 00:00:20,680
in-app purchases button on our storyboard.

5
00:00:21,150 --> 00:00:26,610
And again the point of restoring in our purchases is in the case that someone deletes their app and

6
00:00:26,610 --> 00:00:30,110
they re install it your app and reasons reinstalls it.

7
00:00:30,120 --> 00:00:31,600
They need to be able to get their purchases back.

8
00:00:31,620 --> 00:00:36,480
And this is an apple requirement if you do not implement restore in-app purchases your app will be rejected

9
00:00:36,930 --> 00:00:42,090
of course with consumables or excuse me non consumables with consumables that doesnt matter.

10
00:00:42,090 --> 00:00:48,550
But if you have a one time non-consentual purchase you have to restore it or Apple will reject you.

11
00:00:48,870 --> 00:00:57,660
So put a button up here at the top and take out the word button like so and we are going to go ahead

12
00:00:57,660 --> 00:01:07,740
and put in the restore button here and I'm going to pin it to the top 10 from the right give it a fixed

13
00:01:07,740 --> 00:01:14,100
width and height just make it nice and easy and we're going to open the assistant editor and just simply

14
00:01:14,100 --> 00:01:18,430
make an IB action done this before no big deal.

15
00:01:20,500 --> 00:01:28,580
Put it about this one button and control drag and this is going to be an action and we're going to call

16
00:01:28,580 --> 00:01:30,220
this restore.

17
00:01:30,230 --> 00:01:34,750
But in pressed close the assistant editor.

18
00:01:35,060 --> 00:01:37,230
Let's look at our code here.

19
00:01:37,850 --> 00:01:40,110
Store button press cleanup.

20
00:01:40,400 --> 00:01:43,940
And what we're you going to do is we're going to have a function in our purchase manager so let's pretend

21
00:01:43,940 --> 00:01:52,650
the function exists and we're going to say purchase manager instance dot.

22
00:01:53,210 --> 00:01:56,660
Well this is great at first because we're gonna need to put a callback in there.

23
00:01:56,660 --> 00:01:57,350
It's all good.

24
00:01:57,440 --> 00:01:59,520
Let's go to purchase manager and do that now.

25
00:01:59,900 --> 00:02:09,350
So we'll do the same thing as this but we'll change the name of the function and we're going to call

26
00:02:09,350 --> 00:02:14,120
this store purchases like so.

27
00:02:14,510 --> 00:02:15,760
OK.

28
00:02:15,770 --> 00:02:21,020
And now what we can do is we can go back to the home PC and call that function so will say purchase

29
00:02:21,020 --> 00:02:24,580
manager for instance restore purchases.

30
00:02:24,620 --> 00:02:32,330
And just like before we'll create or little completion handler Here success and there we have it.

31
00:02:32,330 --> 00:02:35,990
So when that's done we'll be able to restore.

32
00:02:36,020 --> 00:02:41,690
And of course if it was failed you could say well you don't have purchases to restore things like that.

33
00:02:41,780 --> 00:02:43,460
So we're just kind of handling all the cases there.

34
00:02:43,460 --> 00:02:45,400
So what do we need to do.

35
00:02:45,500 --> 00:02:51,610
Go back to our purchase manager and actually just write the code to restore it and purchase so purchased

36
00:02:51,620 --> 00:02:54,180
manager or store purchases.

37
00:02:54,530 --> 00:03:03,630
And if Eskay payment you can make payments you always check that.

38
00:03:05,420 --> 00:03:08,160
And what did you say else.

39
00:03:09,320 --> 00:03:16,560
Okay so if we can make payments and then let's go ahead and store transaction complete equals on comp.

40
00:03:16,580 --> 00:03:18,650
let's store that callback handler.

41
00:03:18,800 --> 00:03:19,520
Okay.

42
00:03:19,880 --> 00:03:24,650
And then if it fails or if you can't make payments we'll say on complete and we'll say false.

43
00:03:24,650 --> 00:03:30,380
So they can't make a payment so we obviously can't restore the purchases which is fine and then we're

44
00:03:30,380 --> 00:03:31,110
storing this here.

45
00:03:31,120 --> 00:03:36,270
So let's go ahead and say S-K payment Eskay payment.

46
00:03:36,290 --> 00:03:45,550
Q default at looks at herself as an observer let's say self and then let's say S-K payment.

47
00:03:45,550 --> 00:03:51,560
Q That default restore completed transactions.

48
00:03:51,740 --> 00:03:52,700
Wonderful.

49
00:03:52,790 --> 00:03:58,200
So as similar as we did before we're adding ourself as an observer for this particular payment or transaction.

50
00:03:58,430 --> 00:04:02,120
And then we're calling the restore completed transaction which is going to go to Apple and is going

51
00:04:02,120 --> 00:04:08,870
to do all the work under the hood for us to see which transactions need to be restored if any at all.

52
00:04:09,030 --> 00:04:09,730
Okay.

53
00:04:09,980 --> 00:04:15,010
And so back down here in our restored section now we can just finish this up.

54
00:04:15,020 --> 00:04:16,230
No big deal.

55
00:04:16,340 --> 00:04:25,100
We're going to say if transaction payment type product identifier equals IAP remove ads.

56
00:04:25,520 --> 00:04:31,670
So if it equals the remove ads all we have to do is what we've done before.

57
00:04:31,910 --> 00:04:37,890
Saving the transaction with the user default so we can just copy this line of code here to save that

58
00:04:37,910 --> 00:04:40,370
purchase like so.

59
00:04:40,610 --> 00:04:41,160
OK.

60
00:04:41,480 --> 00:04:44,050
And then we can just call the callback handler.

61
00:04:44,060 --> 00:04:48,430
So transaction what's already here actually right here.

62
00:04:50,590 --> 00:04:51,690
That's cool.

63
00:04:51,770 --> 00:04:55,140
That's yeah I like that a lot.

64
00:04:55,140 --> 00:04:57,210
Let's just leave it there.

65
00:04:57,450 --> 00:04:59,590
We'll leave it there as is.

66
00:05:00,220 --> 00:05:02,790
Because let's assume that there was more than one in that purchase.

67
00:05:02,790 --> 00:05:07,730
You know we would want to do all of them check all of them and then call the transaction complete.

68
00:05:07,830 --> 00:05:09,320
So that's fine.

69
00:05:09,360 --> 00:05:13,550
I like this we just setting the standard defaults to true on this completion Hendershot and then we

70
00:05:13,580 --> 00:05:16,860
go back here to the home visi.

71
00:05:17,100 --> 00:05:18,120
OK.

72
00:05:18,180 --> 00:05:24,340
And what we can do is what we kind of did before up here is we check this.

73
00:05:24,750 --> 00:05:28,290
In fact we can call it we can put this to a function or make it reusable.

74
00:05:28,290 --> 00:05:33,530
So we'll call this function phunk set up ads.

75
00:05:33,890 --> 00:05:34,650
Okay.

76
00:05:35,040 --> 00:05:45,390
And we'll just cut this out of here like so pasted here and then will call set up ads and then here

77
00:05:46,440 --> 00:05:56,310
we will say if success sort of adds We don't know if it fails we don't need to because the ads are already

78
00:05:56,310 --> 00:05:56,850
there.

79
00:05:56,880 --> 00:06:00,070
So if it was successful it set up the ad so it's going to go through here do the check.

80
00:06:00,230 --> 00:06:00,930
Hey does it exist.

81
00:06:00,930 --> 00:06:07,290
If so remove these otherwise load the advertisement which is just what we want to do.

82
00:06:07,830 --> 00:06:08,700
Awesome.

83
00:06:09,240 --> 00:06:10,290
Very very cool.

84
00:06:10,290 --> 00:06:12,790
So let's see if it actually works here.

85
00:06:12,790 --> 00:06:14,110
Got all of our logic in place.

86
00:06:14,100 --> 00:06:20,430
It seems like so what we need to do is actually delete the app from the phone and then reinstall it

87
00:06:20,430 --> 00:06:23,430
and run it with the same user and see if it actually works.

88
00:06:23,430 --> 00:06:25,140
So let's do that now.

89
00:06:25,590 --> 00:06:32,350
So I'm going to delete these here if I can get past the 3D touch which it did.

90
00:06:32,430 --> 00:06:33,700
So those are deleted.

91
00:06:33,990 --> 00:06:36,470
So right the users uninstalled your app.

92
00:06:36,920 --> 00:06:39,540
They later decided you know what I miss the app.

93
00:06:39,630 --> 00:06:42,570
I want to install it again but I Wylma in it purchases what do we do.

94
00:06:42,870 --> 00:06:45,620
So let's go ahead and run it on the phone again.

95
00:06:45,690 --> 00:06:47,970
It does not work on the simulator

96
00:06:50,650 --> 00:06:52,150
it's set up self.

97
00:06:52,180 --> 00:06:55,180
Here we go.

98
00:06:55,300 --> 00:06:58,450
Now let's run it and the app is running.

99
00:06:58,700 --> 00:07:01,020
And of course we see advertisements as expected.

100
00:07:01,020 --> 00:07:05,720
And so what should happen is when I press that button at the top right it should prompt me.

101
00:07:05,750 --> 00:07:07,810
And then my ads should go away.

102
00:07:08,000 --> 00:07:13,290
So press the button.

103
00:07:13,470 --> 00:07:20,320
Well that was fast a little bit too fast don't you think so.

104
00:07:20,440 --> 00:07:23,730
So we let's make sure nothing crazy is going on here.

105
00:07:23,770 --> 00:07:25,500
So we clicked restore purchases.

106
00:07:25,540 --> 00:07:31,030
We went to the purchase manager K and we typed restore purchases.

107
00:07:31,040 --> 00:07:32,170
If we can make payments great.

108
00:07:32,180 --> 00:07:34,720
Otherwise uncomplete was false.

109
00:07:34,730 --> 00:07:35,460
OK.

110
00:07:35,810 --> 00:07:40,260
And so uncomplete our code here let's check it out.

111
00:07:42,300 --> 00:07:44,430
If success is true we'll set up the ads.

112
00:07:44,440 --> 00:07:48,030
Well it wasn't true there so we're good there.

113
00:07:48,270 --> 00:07:53,950
And then we went through here to add and restore the completed transactions and went through here restored.

114
00:07:54,060 --> 00:07:54,780
We finished it.

115
00:07:54,780 --> 00:08:01,180
And if the payment product identifier is removed the adds then let's set it to true.

116
00:08:01,260 --> 00:08:03,240
Wow that was fast so it all worked.

117
00:08:03,240 --> 00:08:04,100
That's pretty cool.

118
00:08:04,110 --> 00:08:06,920
I thought it would take a little bit longer but it worked.

119
00:08:06,960 --> 00:08:10,910
It worked without even prompting for a password or anything like that which is really awesome.

120
00:08:10,920 --> 00:08:14,570
Apple knows which device it is under the hood and made it work for us.

121
00:08:14,570 --> 00:08:15,680
So there you have it.

122
00:08:15,690 --> 00:08:19,170
You've now learned how to work with in-app purchases you've learned how to set up your accounts on iTunes

123
00:08:19,170 --> 00:08:22,200
and developer to Apple dotcom how to get your apps to sink to that.

124
00:08:22,200 --> 00:08:26,420
Don't forget to turn on the capabilities on your project.

125
00:08:26,480 --> 00:08:31,280
Clicking the capabilities tab here you learn how to integrate Google AdMob and how to get better at

126
00:08:31,290 --> 00:08:31,810
showing.

127
00:08:31,830 --> 00:08:36,570
And then you learn how to manage your code so when you make that purchase you actually implement the

128
00:08:36,570 --> 00:08:40,980
feature and how to restore the in-app purchases in the case that an app is deleted and then re-installed

129
00:08:41,040 --> 00:08:42,580
a lot of good stuff here.

130
00:08:42,630 --> 00:08:46,170
We're only scratching the surface though and in a purchase purchases you can do a lot more with subscriptions

131
00:08:46,680 --> 00:08:50,050
and non and consumables as well too.

132
00:08:50,250 --> 00:08:51,750
But enough to get you started.

133
00:08:51,750 --> 00:08:52,770
So that's it for now.

134
00:08:52,770 --> 00:08:54,990
Marc price dove slopes dot com.

135
00:08:55,020 --> 00:08:55,800
See you next time.

