12/08/09
Using Store Kit Framework
One of the new framework in firmware 3.0 is the Store Kit. It can be very usefull to get a new business model in iPhone applications. For example, you can create a subscription magazine app where you ask for payment on a monthly, yearly or periodic basis of your choice. Sell extra levels to extend the experience of your game,…
The main steps to use this framework are:
- - Create “In App Purchase Test User” in iTunes Connect Portal.
- - Have create App ID in the Developer Portal and enabled in-App purchases for that.
- - Create the new App on in iTunes Connect Portal, and Register a product for this App.
- - Develop the store in your App.
1.- Create “In App Purchase Test User” in iTunes Connect Portal.
Go to iTunes Connect Portal, (https://itunesconnect.apple.com), and click the option “Manage Users”, once on there, choose “In App Purchase Test User” option, and add a new User. The email that they requiere must be different of your iTunes Connect mail, it can be a dummy mail.
iTunesConnect->Manage Users-> In App Purchase Test User-> Add New User.
2.- Have create App ID in the Developer Program Portal and enabled in-App purchases for that.
Go to Developer Program Portal (http://developer.apple.com) and select “App IDs” section. The next step will be create a new App ID. Select the top right button “New App ID”. In bundle identifier I recomend to use a string like this, “com.yourcompany.appname”.
Developer Program Portal -> App IDs -> New App ID.
3.- Create the new App on in iTunes Connect Portal, and Register a product for this App.
Now we create the app on iTunes Connect. Again go to iTunes Connect Portal and select “manage your applications”. On left top you can see the “add new application” button, click it, and follow the steps. On Pricing step, it’s very important that you select a payment application (Not free), cause the app purchases are only available on this kind of App.
iTunes Connect -> Manage your Applications -> Add new Application
Once the App has been created, you can see it on “Manage your applications”. To add In-app purchase, select the button “Manage in-app purchase”. In this page, you can create your app purchases using the top left button, “Create New”.
iTunes Connect -> Manage your Applications -> Manage in-app purchase.
* If your application is free, you can’t see this option
4.- Develop the store in your App.
This is the final step to use store kit. First of all it’s very important decide your application store model. Depending on how many items your store offers and how often you add new items, you may want to use one of two basic models: self-contained or downloadable content. In this tutorial we use a self-contained model.
In XCode, when you set up the project, make sure to link to StoreKit.framework.
Store Kit does not allow your application to patch itself or download additional code. For this the new feature must already exist in your application waiting to be unlocked
4.1.- Get the in-app purchase.
If you want to check the in-app purchase on your iphone app.
DummyView.h
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
@interface DummyView : UIView <SKProductsRequestDelegate> {
}
- (void) requestProductData;
@end
DummyView.m
#import “DummyView.h”
@implementation DummyView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self requestProductData];
}
return self;
}
- (void)dealloc {
[super dealloc];
}
- (void) requestProductData
{
SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: @"com.yourcompany.product "]];
request.delegate = self;
[request start];
}
//***************************************
// PRAGMA_MARK: Delegate Methods
//***************************************
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *myProduct = response.products;
// populate UI
for(int i=0;i<[myProduct count];i++)
{
SKProduct *product = [myProduct objectAtIndex:i];
NSLog(@”Name: %@ - Price: %f”,[product localizedTitle],[[product price] doubleValue]);
NSLog(@”Product identifier: %@”, [product productIdentifier]);
}
}
@end
4.2.- Buy a in-app purchase.
First of all you need register a transaction observer with the payment queue. For this you must create a instance class like this.
CustomStoreObserver.h
#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>
@interface CustomStoreObserver : NSObject<SKPaymentTransactionObserver> {
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;
- (void) failedTransaction: (SKPaymentTransaction *)transaction;
- (void) restoreTransaction: (SKPaymentTransaction *)transaction;
- (void) completeTransaction: (SKPaymentTransaction *)transaction;
@end
CustomStoreObserver.m
#import “CustomStoreObserver.h”
@implementation CustomStoreObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
}
}
- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
if (transaction.error.code != SKErrorPaymentCancelled)
{
// Optionally, display an error here.
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
//If you want to save the transaction
// [self recordTransaction: transaction];
//Provide the new content
// [self provideContent: transaction.originalTransaction.payment.productIdentifier];
//Finish the transaction
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
//If you want to save the transaction
// [self recordTransaction: transaction];
//Provide the new content
//[self provideContent: transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
@end
And in DummyView.m you can add a button with this Event Method to buy in-app purchase.
-(void) subscribe:(id) sender{
SKPayment *payment = [SKPayment paymentWithProductIdentifier:productIdentifier];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
This is a simple tutorial to take a first look to Store Kit. I hope that it will be usefull for you.
16 Comments
RSS feed for comments on this post. TrackBack URL
Sorry, the comment form is closed at this time.
I followed your steps word for word. but when i query for the products, the size (count) of response.products is always zero!
I copied and pasted the product ID’s from from iTunes connect into my app, and still nothing.. I created the test user, nothing..
is there anything else that I forgot?
Comment by Jay by 18/08/09 @ 21:44
Hi,
In iTunes Connect, in the section “Manage Your In App Purchases” select your App and then select your “In App Purchase” and Check if “Cleared for Sale” is YES. If not, you can select it when you edit this in-app purchase.
Regards.
Comment by Tesh by 24/08/09 @ 08:04
Hi there,
Cool post, I just stumbled upon it and I’m already a fan.
Comment by Computers & Tech by 18/09/09 @ 00:08
I have question, I have my app that is .99 $. do I need some code in my app to be able to charge for it. do I need the StoreKit on my app and aditional code?
thanks
Comment by Edgard Rodriguez by 01/10/09 @ 22:00
Great tutorial! What would you change to use downloadable content?
Comment by ian by 09/10/09 @ 23:28
> I have question, I have my app that is .99 $. do I need some code in my app to be able to charge for it. do I need the StoreKit on my app and aditional code?
You only need to do this stuff if you will be charging for other things (besides the purchase of your app) from within your app.
If you simply want people to have to buy your app (in your case, for $0.99), then you don’t have to deal with this stuff. You do, however, have to configure your app for sale in iTunes Connect.
Comment by foobar by 12/10/09 @ 18:26
I *finally* got the first part to work (getting a list of products back).
It wasn’t working when I used
initWithProductIdentifiers: [NSSet setWithObject: @"com.yourcompany.product "]];
I tried every permutation of “com.yourcompany.product” and “productid” and nothing worked. Finally, I got rid of the “com.yourcompany.product” part and just used the “productid” part
initWithProductIdentifiers: [NSSet setWithObject: @"productid"]];
It’s been a very frustrating day, but now I’m happier.
Comment by foobar by 12/10/09 @ 19:12
In case it wasn’t clear, what I mean by “productid” is the value for productid that you see in iTunes Connect, *not* the literal string ‘productid’. Eg: my test product id is “i1234″, so the line that worked for me was
initWithProductIdentifiers: [NSSet setWithObject: @"i1234"]];
Comment by foobar by 12/10/09 @ 19:15
I was working on the 2nd part (4.2.- Buy a in-app purchase.) and I was not seeing any of the delegate methods getting called (eg: paymentQueue updatedTransactions, failedTransaction, restoreTransaction, completeTransaction) and then I realized that it was because I had not set anything up to be a transaction observer. My main view class is what I had set to be both a SKProductsRequestDelegate and a SKPaymentTransactionObserver, so I just added the following line to my initialization sequence and, voila, I was seeing the delegate methods getting called once I initiated the payment.
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
Thanks for the tutorial.
Comment by foobar by 12/10/09 @ 19:59
Why is this page such small?
I can read hardly.
Comment by why by 16/10/09 @ 07:56
Hi, it is small for design issues, but I’ve to say that we are planning to make this particular page more like a blog and leave it grow.
Comment by Marc by 16/10/09 @ 08:00
Excellent post, very helpful. The only missing part is creating and adding the transaction observer to SKPaymentQueue.
[code]
storeObserver = [[CustomStoreObserver alloc] init];
[[SKPaymentQueue defaultQueue] addTransactionObserver:storeObserver];
[/code]
Comment by Isaac by 20/10/09 @ 14:31
hi
thanks for this useful post. but i have a big problem, when “void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response” called, the product is null, do you know why? i do every step exactly
Comment by Mina by 25/10/09 @ 10:45
Hi,
I want to one thing about in-app purchase that how it will handle price? Can we setup different prices for different countries?. How it will cut the price?.
Thanks.
Comment by iPhone Coder by 02/11/09 @ 19:23
my requirement is such that I need to get payment from user upon successful receive of payment I need to alter current application (not to download newer one but update the application) i will enable certain things in my app
how to code this payment scenario ?
what i want to know is how can i use storekit framework to get payment from the buyer , just want sample code or any initiative thing to move ahead…
Comment by abc by 08/02/10 @ 07:58
I gone thru step-4 but when I do click on build and go, it give me error like “productIdentifier” undeclred (first use in this function).. How to get resolve this.?
Comment by abc by 08/02/10 @ 08:00