IOS Development Beginner: In-app purchase In-app Purchase

Source: Internet
Author: User

http://blog.csdn.net/songrotek/article/details/8680415

There are many applications that use the In-app Purchase, although for many users, they may not like or even hate this mode, thinking that one click will be deducted from the account. However, in-app purchases can be a good business model for developers, and people will increasingly accept this purchase pattern.

Let's start by introducing the fundamentals and programming methods for in-app purchases.

1. Basic Principles

Here's a reference to Apple's development documentation In-app PURCHASE Programming Guide

Briefly describe the process:

Pre 0: Create the appropriate product for the app in Itunesconnect and add these products to the app information. Introduction after the specific steps.

Step 1: Get a list of products within the app based on the bundle identifier of the product created.

Step 2: Apply the requested product information. Product information is Skproduct object.

Step 3:app Store returns information. In practical programming, step1,2,3 are together. Get skproductsresponse by creating skproductsrequest. The skproducts information is in the object of Skproductsresponse and is its property.

Step 4: Display product information to the user in the application

Step 5: The user clicked on a product.

Step 6: App sends a purchase request to the App Store Payment request

The Step 7:app store processes the request, completes the transaction, and returns information to the app.

Step 8: The app gets the information and then unlocks the purchased content to the user based on the transaction situation.

This is a basic process description of the In-app purchase. In the course of our actual programming. For this product list, we may choose to provide it directly to the user, rather than getting information through the App Store. Only when a user clicks on a product do we start to get product information and complete the purchase. On the other hand, in the purchase process, we should display enough information in the application, so the notification in the course of the transaction is also very important.

The following begins stepbystep to introduce the entire concrete implementation process. Here is just the basic implementation method, take non-consumable product as an example.

Step 1: Create a product

The first thing to illustrate is that in order to achieve in-app purchases, your AppID is that com.companyname.appname must be unique and cannot take *.

Select Manage In-app Purchases in manage My applications in Itunesconnect

There are four types of products, as detailed in the development documentation. The choice of non-consumable here is to buy a lifetime product. Consumable is the consumer can continue to buy, which is more common in the game.

Above is the product details fill in. Here special attention is ProductID fill, is actually productidentifier, this and application of bundleidentifier similar, must be unique, The general practice is to fill in the Com.companyname.appname.productname, of course, in essence can be any string, as long as unique. This ProductID is the basis for obtaining product information later in the program. The rest of the information is very simple to fill out, here is not the fee statement.

Step 2: Add products to the application version information

Go to the App page, click View Detail, and see below

In-app Purchases, click Edit and add the products you created earlier.

Step 3: Create a test user

In order to test In-app purchase,apple in the development phase, we provided the test user feature, which allows you to use this account for free in-app purchases at development time.

Specifically on the Itunesconnect home page, click Manage Users

Click Test user to create it.

Step 4: Start programming. To add Storekit.framework in Xcode, use it to implement the function

Step 5: Generally we will create a separate class to implement in-app purchases. Since this is a tutorial, not a case, it is not intended to move the entire class into the program. Just to introduce important things and processes.

To join in a class

<SKProductsRequestDelegate,SKPaymentTransactionObserver>

For skpaymenttransactionobserver this thing can monitor the entire process of the transaction, even if the transaction exits the application, the transaction can continue, of course, to return to the application of the page in order to finalize the transaction, display product corresponding content. The initialization of the class should be added to the

[[Skpaymentqueuedefaultqueue] addtransactionobserver:self];

Add this code to achieve the function of Transactionobserver, the corresponding methods can be added later

Paymentqueue: Opening

Step 6: The following introduction is not limited to the writing of a class, but follows the purchase process. Let's say we've written an in-app purchase class, and then we're going to make a purchase. The first is the request products.

_productrequest = [[Skproductsrequestalloc]initwithproductidentifiers:_productidentifiers];

_productrequest.delegate =self;

[_productrequest start];


A complete request as above, for productsidentifiers, this is a set, which is to create a set here to join each Productidentifer

Nsset *productidentifiers = [nssetsetwithobjects:

K_camera_angle_mode,

K_slope_angle_mode,

K_dihedral_angle_mode,

K_line_plane_angle_mode,

NIL];

And then the request delegate's methods.

-(void) Productsrequest: (skproductsrequest *) Request Didreceiveresponse: (Skproductsresponse *) response

{

Nsarray *skproducts = response.products;

Process ....

}

-(void) Request: (Skrequest *) Request Didfailwitherror: (Nserror *) error

{

Process ....

}

The request succeeds, can obtain the products, a nsarray, inside is the Skproduct object product information. Product information has name, price, etc., it is easy to find. These things are only useful when displaying information, not required at the time of purchase, just use Skproduct.

Step 7: Purchase

Skpayment *payment = [skpaymentpaymentwithproduct:product];

[[Skpaymentqueuedefaultqueue] addpayment:payment];

Code as above. Then you start connecting to the App Store. Main look below

#pragma mark-skpaymenttransactionobserver

-(void) Paymentqueue: (Skpaymentqueue *) queue updatedtransactions: (nsarray*) transactions

{

For (skpaymenttransaction *transactionin transactions) {

Switch (transaction.transactionstate) {

Caseskpaymenttransactionstatepurchased:

[Selfcompletetransaction:transaction];

Break

Caseskpaymenttransactionstatefailed:

[Selffailedtransaction:transaction];

Break

Caseskpaymenttransactionstaterestored:

[Selfrestoretransaction:transaction];

Default

Break

}

}

}


Here's the third trading status above restore, recovery. This is the case. If someone buys a product on an iphone with an account, then download the app on the ipad and buy it again? No, through restore in the App Store to detect your purchase record of this account, if there is a purchase record exists, then do not have to buy again, direct restoretransaction.

The next step is to deal with the purchase status separately.

-(void) Completetransaction: (skpaymenttransaction *) transaction{
    Your application should implement these, methods.    [Self recordtransaction:transaction];    [Self provideContent:transaction.payment.productIdentifier];
    Remove the transaction from the payment queue.
    [[Skpaymentqueue Defaultqueue] finishtransaction:transaction];}

We usually use Nsuserdefaults to make a record of the transaction.

Note that this line of code is finishtransaction in the program so that Transactionobserver no longer monitors the transaction.

Other state restore,failed are similar to the processing, which is available in the development documentation.

Of course, for restore, there is one more method to note

-(void) Paymentqueue: (Skpaymentqueue *) queue restorecompletedtransactionsfailedwitherror: (Nserror *) error

If restore fails, you can display the appropriate informational prompt

Step 8: Additional

Throughout the purchase process, we usually give users a hint of information, such as waiting, such as connecting, such as the transaction has been completed. To achieve these functions, the notification should be used to add postnotification to the above trading links, and then to effectively handle the notification. This article only talk about In-app Purchase, about notification programming not to do introduction.

Basically, you can complete the entire in-app purchase through the above link. Of course, from a security consideration, Apple sends a verification message after the transaction is completed, confirming the legality of the transaction by sending a verification message to the App Store to determine whether the transaction originated from the App Store.

In this regard, Apple has a code package that provides validationcontroller for easy validation.

IOS Development Beginner: In-app purchase In-app Purchase

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.