Before proceeding, I am writing my experience with In-App purchases which I gained from developing a game recently. So, the idea presented here is actually implemented in a “real-world” app as they call it, but still!! No guarantees given!!
So, there are a lot of good, simple online tutorials about in-app purchases already, right? WRONG! :p
Of course, the famous Ray Wenderlich website does have a good tutorial, but, you guessed it, it wasn’t simple enough for me.
So, what is the level of simplicity are we talking about here? It is a 1 in-app non-consumable in-app purchase simple!
Ok, about time we start!!
First, you have to set up your in-app purchases in iTunes Connect. Very straight forward stuff. Check elsewhere if you have any difficulty.
Then, dive into your code! … Or before that, link the StoreKit framework. We’ll need it.
Ok, I think it is pretty straight forward? Somewhere in your app, you want to restrict the access of the user if he didn’t purchase the IAP. That’s basically how you check that. call NSUserDefaults and check the key!
Ok, now, what if he didn’t purchase the IAP??
What I do first is explain to the user that if he deleted and reinstalled the app, his IAP will be available free of charge, even if it says that he’ll have to purchase it again. After that, I check if the user is connected to the Internet using the Old reachability class from apple. Finally, before initiating the IAP, I display some indicator that the app is contacting IAP servers.
Now, to contact the IAP servers: I have the following code:
That’s a singleton class that handles IAP’s. Reason it is a singleton? Reusabliity! I can just drop it in any project and access the class from anywhere. So, Let’s start dissecting the PurchaseHelper:
First: The Purchase Helper Protocol:
Straight forward protocol that informs the delegate about the IAP result.
Second: Public mehods
We already saw where we call requestInAppPurchase, and the cancel method is called when the user decides to cancel while we are displaying the activity indicator..Obviously enough. Now, the requestInAppPurchase method:
First, we wrap our only IAP ID into a set. The ID can be obtained from iTunesConnect after you set up your IAP. Then, we initialize an SKProductsRequest object with the wrapped object. Finally, set the delegate and start!
Third: Delegate mehods
Now, we have to implement the delegate methods we troubled ourselves with. Those are for:
And.. the functions:
So, what do we need to take note of here??
The request you send will receive a response, calling: productRequest:…
Defensive programming!! Remind your self that you are supporting a single IAP with a nice NSAssert.
Set up the paymentQueue exactly the way I did. (aka Copy/paste).
The Payment Queue delegate method is called: paymentQueue:…
In the switch statement, we only use: SKPaymentTransactionStatePurchased and SKPaymentTransactionStateFailed.
In case of failing, just inform the delegate and reset everything.
In case of Purchased, call the finishedTransaction (as writtten), then set your NSUserDefaults to save the purchase. Finally, inform your delegate.