Now, in the last lesson, we looked at how we can implement in-app purchases and how we can test it using 1
sandbox users. 2
Now, in this lesson, we're going to enable the user to access whatever it is that they purchased namely 3
the premiumQuotes 4
once we get a successful transaction reported back to us from the paymentQueue. 5
So in order to do that, we're going to add a method here that calls showPremiumQuotes, and then I'm 6
going to create it down here function showPremiumQuotes and that error should go away now. 7
Now, how do we show Premium Quotes? 8
Well, currently, they have access to these quote and we want them to have access to these quotes in addition, 9
so we can append this array quotesToShow with the quotes inside the premiumQuotes so that we add 10
all of these quotes--so that we add all of these quotes to this array and we render all of that in our 11
table view. 12
And that is why quoteToShow is a variable and premiumQuotes is a constant. 13
So let's go ahead and do that inside here. 14
So once the transaction is successful, we're going to trigger the showPremiumQuotes and we're going 15
to find the array that is quotesToShow and we're going to append, not a new element, because that takes 16
a single string, 17
so that's like adding just one string item to the end of the array. 18
But this one, 19
append 20
contents of sequence. 21
So this allows us to tag on the contents of our premiumQuotes array into the quotesToShow array. 22
So let's go ahead and hit enter. And the content that we want to tag on is called premiumQuotes. And this 23
line of code will add all of these quotes to the end of all of these quotes. 24
And now we have a giant quotes to show that we're going to render in the table view. 25
So because our table view is already calculating how many cells to show based off this dynamic variable, 26
the number of items inside quotesToShow which now equals to 12, then we don't actually have to change 27
any of this code, 28
nor this code because it's also rendering all of the items inside quotesToShow based off the current 29
indexPath.row that needs it. 30
But what we do need to do is to call reloadData on our tableView, so that these two methods gets triggered 31
again and that we get the updated data and the updated number of cells rendered. 32
So, now let's test it on our app. 33
Now, remember that because we've bought this product with our last sandbox user, we're going to have to 34
use a new sandbox user. 35
So I'm going to go into Setting and, again, iTunes & App Store, and I'm going to sign out of my previous 36
tester, and I'm going to sign in with a new tester 37
who hasn't bought this item before. 38
And now, I'm going to run the app again and really click on get more quotes. And I'm going to enter that 39
password for that sandbox user once more. 40
And now, I get the confirmation that my transaction was successful and I've got all of the rest of the 41
quotes. Now at this point, you might notice that there's something a little bit weird. 42
Why is it that every so often, you see some of these cells get becomes lightly blue colored, and they 43
have the disclosure indicator on it like the one that we implemented for the very last cell? 44
Well, the reason is because our cells are getting reused. So every time I push the cells off the table view, 45
they come back at the bottom. And equally, when I scroll them down off the table view, they come back 46
at the top. 47
So in order for this to not happen, we have to explicitly set the cells that are meant to be normal quote 48
cells to have a black text color, so cell.textLabel.textColor is equal to color literal that 49
is just going to be black, 50
and the cell should have an accessoryType of none, 51
so remove all of its accessoryTypes. And now, if we rerun our app and click Get More Quotes, enter a password,V 52
then we'll restore all the items that we bought before, and now you can see it doesn't do that anymore. 53
The parts that are meant to be black are black, and the part that's meant to be blue is blue. 54
Now, the next thing we need to address is when a user opens up the app, we should check to see if they've 55
already purchased the in-app purchase, in which case, we'll show them the premium content, or if they haven't yet 56
purchased it, then we'll keep it exactly the same as before. 57
Now, in order to keep hold of that data or whether if they purchased the in-app purchase or not, then we can use 58
our UserDefaults that we learned about in the Todoey module. 59
Now, in order to do this, whenever a successful transaction goes through, in addition to showing the premium 60
quotes and ending the transaction, I'm also going to tap into the UserDefaults.standard object 61
and I'm going to set a Boolean. 62
So this case, the value is a Boolean for a key, and the Boolean is going to be true because the key is 63
going to be our in-app purchase. 64
So the key is going to be this product ID here that relates to the item that they purchased namely 65
premiumQuotes. 66
So at a later date, I want to be able to tap into the UserDefaults and use my key of my product ID 67
and check to see whether if that product was purchased or not. 68
So, now that we've set that UserDefault, we can now create a function code isPurchased, and this function 69
is simply going to return a Boolean, 70
so a true or false. 71
And we're going to use this function whenever we need to check whether if the user has purchased the premium 72
quotes or not. 73
So let's say let purchaseStatus = UserDefaults.Standard.bool forKey, 74
so we grab the Boolean for the key that is our product ID to check to see if they purchased it or not. 75
If this went through successfully once at some point in the lifetime of using our app, then this Boolean 76
will be true. 77
And we can now use this purchaseStatus to check if purchaseStatus is true, 78
so you can either say is equal to true. Or the shorter way, 79
then we can simply just say if purchaseStatus is true, then we can print "Previously purchased," and we 80
can also return true for this method of isPurchased. Now, else, i.e., if it wasn't ever purchased, then we'll 81
print "Never purchased" 82
and we will return false. 83
So, now we have this method in order to check whether if the user has purchased this set of premium quotes 84
or not. 85
So in our viewDidLoad, 86
we can already call this method, we can say if isPurchased is true, then, 87
in that case, we're going to straight away showPremiumQuotes. And that means that whenever the user 88
launches the app, say, if they force quit it before or the app was closed and shut down, then whenever 89
they open up the app and viewDidLoad gets called, we'll check to see if they've already purchased the 90
premium quotes, and if they have, we'll straightaway show them the premium quotes. 91
So let's again test this in our app. 92
So even though previously, we have successfully purchased this in-app purchase because we didn't set 93
that UserDefault last time, we need to set it now. 94
So you'll need to go into Settings, iTunes & App Store, and, again, get a new ID for your sandbox account, 95
and log in to that new ID 96
the person who's never bought your in-app purchase. 97
So in my case, it's tester2@lab.com, and I'll just enter the password and hit sign in. 98
So, now that I'm signed in, I'm going to go back into my app and click Get More Quotes to buy it, 99
and this is a first purchase. 100
So I'm going to enter my password, again, to confirm the purchase. And three, two one, you're all set and 101
I've got all of those quotes now added in. 102
So, now if I force quit my app and come back to it, you can see that as soon as I load it up, it will have 103
all of those quotes. 104
And, again, if I terminate the app and run it again from Xcode, you can see straight away it has all of 105
the quotes that I've bought which is brilliant. 106
There's nothing like an upset user who's already paid for the content and isn't getting access to it. 107
All right. 108
Now, there's just one last thing that's a little bit niggling which is right at the bottom here, 109
we've still got that cell that says "Get more quotes," even though we've already gotten all the quotes. 110
We actually don't have any more quotes to give them. 111
So let's see how we can fix that. 112
Now, we have this function called isPurchased, so we can check to see whether they've already purchased 113
the premium quotes and if they have purchased the premium quotes, then we're going to say if isPurchased 114
is true, then we're only going to return the number of cells that quotesToShow has. And that will contain 115
all 12 quotes. 116
However, if they have not yet purchased the premium quotes, then we're going to return one extra cell, 117
and that, of course, includes the last cell which has that "Get More Quotes" button. 118
And now if we run our app, then you can see that we only have 12 quotes and 12 cells when we have purchased 119
our in-app purchase, and our app has a much less confusing user experience. 120
Now, in the next lesson, I want to talk about the last thing that's important to in-app purchases which 121
is restoring user's previous purchases. 122
So for all of that and more, 123
I'll see you on the next lesson.