All right. 1
So we've achieved a lot already with not many lines of code. We've implemented in our in-app purchase and we've 2
also given the user access to their purchases for now and forever in the future. 3
Now, there's just one problem. 4
What happens if, say, the user gets a new phone or the user updates to the latest iOS version, 5
then, as you know, some of the apps gets deleted permanently, and that means our UserDefault for the 6
app are also wiped clean? 7
So how do they get access to their purchase product? 8
Well, that is where restoring in-app purchases comes in. An apple when they review your app with an in-app 9
purchase 10
are very, very militant about the restore purchase button. 11
I've had long e-mail chains with Apple employees just trying to show them where they can find my restore 12
button. 13
So make sure that it's somewhere prominent or make sure you show them where it is in your app or tell 14
them where it is in your app when you send it to them for review, because you can end up with long delays. 15
I think it was something like two or three weeks before the app got approved because it had a slightly 16
hidden restore button. 17
But, essentially, what Apple wants to do is to ensure that their users get the best experience and they 18
don't get tricked into making an in-app purchase, 19
but then not being able to access that purchase when they update their iOS or get a new phone. 20
So for that, we've already included an IBAction right at the top of the navigation bar that says Restore, 21
so that's pretty clear. 22
And you don't have to make it this clear because, obviously, it's a little bit, too, in your face, and you 23
can actually hide it inside a menu or something, and just make sure that you tell the Apple employee 24
when you are uploading it for publishing where that Restore button is. 25
Now when the user presses the restore button, we want to trigger a process of checking their current user 26
ID and checking it against the database on Apple's servers to see whether if they've previously purchased 27
this in-app purchase before. 28
And if they have, then we're going to give them access to the product straight away. 29
So the code that enables us to do this is, again, using that 30
SKPaymentQueue default object and we're going to tap into restoreCompletedTransactions which asks the payment queue 31
to restore previously completed purchases. And this hits up Apple servers, checks the user's Apple ID against 32
all of the purchases they've made before. 33
And if the product with this ID has already been purchased, then it will come back and trigger this payment 34
delegate method, and it will tell us that that has happened. 35
So we can catch that using another "else if" saying, if transaction.transactionState == .restored 36
which is the message that we get sent back when this is successful after checking against Apple 37
servers, and then this is where we restore their purchase. 38
And in our code, that just means trigger showPremiumQuotes, and then print "Transaction restored," then 39
terminate the transaction just as we did before. 40
So SKPaymentQueue.default().finishtransaction, and the transaction we want to end is the one 41
that is currently being reported back. 42
So let's hit run and check that out. 43
Okay. So you can see that, currently, because the app was previously installed, 44
you can see that I'm not being told to buy more quotes and I've got access to all 12 quotes. But let's 45
say, if I delete this app, so I'm going to long hold on it and I'm going to delete it, 46
and now if I run this app again onto my phone, then I shouldn't have a copy of the User Defaults. 47
And you can see that it's gone back to the previous state before I made that purchase. 48
So now let's tap on this restore button to trigger our IBAction and start restoring. 49
And you can see I've already got printed transaction restored and I've now got access to all 12 quotes. 50
Now, currently, the only place where we're setting the UserDefaults to true for our product being purchased 51
is inside this part 52
when a successful transaction or successful payment is made. But if you think about it, 53
let's say, the user buys a new iPhone and downloads this app again, now that UserDefault is going to 54
be wiped clean. 55
So even if they restore their purchase, if we don't set this UserDefault as well, then the next time 56
they open up the app once it's gone into the background, then they'll still have the same problem. 57
So instead of having our UserDefaults up here inside the block of code when the user makes a successful 58
transaction, we're going to cut it and we're going to paste it into this function showPremiumQuotes 59
and we're going to put it right at the top, 60
and especially before we reload the data. Because when we reload the data, we need to make sure this has 61
already been set to true, so that when we check how many cells we need to show, this has to be true in 62
order for us to return the right number of cells 63
without that last buy button. There's just one last user experience sort of tiny thing that we could 64
do is we should probably remove the restore button once it has been restored. 65
So in order to do that, if the restore was successful, then in addition to all of this, we're also going 66
to tap into the navigation item and, namely, the one that we want is the right bar button item, and we're 67
going to set the right bar button item which currently has that button which is the Restore button, 68
we're going to set it to nil to remove it, basically, to have no right bar button items, and we'll set 69
animated to true. 70
So, now--so now when I delete this app and I run it again, and I hit the Restore button, once it's been successfully 71
restored, that confusing button goes away. 72
So I hope this has been fun and you've learned how to implement in-app purchases in your apps and start 73
generating more revenue for all of the apps that you create. 74
And we've gone through the process of how to restore transactions, how to process transactions, how to 75
set up in-app purchases, how to process in-app purchases, and how to restore transaction. 76
So that's all from me in this module, and I hope to see you on the next lesson.