In-App purchase

In-App purchase is a big thing for all the iPhone developers. Not only it helps to tease users to try and buy but also can help to reduce piracy. This is why you should consider it in your next application.

Outline

This post is continuation of In-App Purchase: Quick Overview . This part focus on programming details. The outline of the steps to create and sell products is:

  • create a test user in iTunes Connect Portal,
  • define the application in the Developer Portal and enable in-app purchases,
  • register new application in iTunes Connect Portal and a product (products) for it,
  • develop the store within your application.

Create a test user in iTunes Connect Portal

Go to iTunes Connect Portal, (https://itunesconnect.apple.com), and click the option “Manage Users”, then choose “In App Purchase Test User” option and add a new User. Following screen shots will leave no doubts



Define the application in the Developer Portal and enable in-app purchases.

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 recommend 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.

? Download DummyView.h
1
2
3
4
5
6
7
8
#import 

#import
 
@interface DummyView : UIView {
}
 
- ( void ) requestProductData;
@end
? Download DummyView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#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& lt;[ 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.

? Download CustomStoreObserver.h
1
2
3
4
5
6
7
8
9
10
11
12
#import 

#import
 
@interface CustomStoreObserver : NSObject {
}
 
- ( void ) paymentQueue: ( SKPaymentQueue * ) queue updatedTransactions: ( NSArray * ) transactions;
- ( void ) failedTransaction: ( SKPaymentTransaction * ) transaction;
- ( void ) restoreTransaction: ( SKPaymentTransaction * ) transaction;
- ( void ) completeTransaction: ( SKPaymentTransaction * ) transaction;
 
@end
? Download CustomStoreObserver.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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.

? View Code OBJC
1
2
3
4
-
(
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.

你可能感兴趣的:(计算机-iphone)