Many applications now use in-APP purchase. Although many users may not like or even hate this mode, they think that they will deduct money from their accounts when they click. However, in-APP purchase is a good business model for developers, and people will increasingly accept this purchase model.
The following describes the basic principles and programming methods of In-APP purchase.
1. Basic Principles
See the apple development documentation in-APP purchase programming guide.
Brief Introduction to the entire process:
Pre 0: create corresponding products for the app in itunesconnect and add these products to the application information. The detailed steps are described later.
Step 1: Obtain the product list based on the bundle identifier of the created product in the application.
Step 2: The application requests the product information. The product information is a skproduct object.
Step 3: App Store returns information. In actual programming, step 1 and Step 2 are together. Create skproductsrequest to obtain the skproductsresponse. The skproducts information is in the skproductsresponse object and is its property.
Step 4: display product information to users in applications
Step 5: The user clicks a product.
Step 6: The app sends a purchase request payment request to the app store.
Step 7: The App Store processes the request, completes the transaction, and returns the information to the application.
Step 8: The app obtains information and then unlocks the purchased content to the user based on the transaction situation.
This is a basic process description of In-APP purchase. In our actual programming process. For this product list, we may choose to provide it directly to users instead of getting information through app store. Only when a user clicks a product can we get the product information and complete the purchase. On the other hand, during the purchase process, we should display enough prompt information in the application, so the notification in the transaction process is also very important.
Next, let's start step by step to introduce the entire implementation process. Here is just the most basic implementation method, taking the non-consumable product as an example.
Step 1: create a product
First, you must note that your appid is com. companyName. appname must be unique and cannot contain *.
In itunesconnect, choose manage in-app purchases in manage my applications.
There are four product types. For details, see the development documentation. Here, we select non-consumable, which is a product that can be used for life. Consumable can be purchased continuously by consumers, which is common in games.
The above is the detailed information of the product. Here, we pay special attention to the filling of productid, which is actually productidentifier. This is similar to the bundleidentifier of the application and must be unique. The general practice is to enter it as COM. companyName. appname. productname, of course, can be any string in essence, as long as it is unique. This productid is the basis for obtaining product information in the program. It is easy to fill in other information, which is not covered here.
Step 2: add the product to the application version information
Go to the application page, click View Detail, and you can see below
In-app purchases, click Edit and add the previously created products.
Step 3: Create a test user
To test in-APP purchase in the development phase, Apple provides the test user function, which allows you to use this account for free purchase during development.
Click Manage users on the itunesconnect homepage.
Click test user to create the instance.
Step 4: start programming. Add storekit. Framework to xcode to implement functions.
Step 5: We generally create a separate class to implement the in-APP purchase function. Because this is a tutorial, rather than a case, we do not intend to write the entire class. It just introduces important things and procedures.
Add to class
<Skproductsrequestdelegate, skpaymenttransactionobserver>
For skpaymenttransactionobserver, You can monitor the entire transaction process. Even if you exit the application during the transaction, the transaction can continue. Of course, you must return to the application page to complete the transaction and display the corresponding content of the product. Class initialization should be added
[[Skpaymentqueuedefaquequeue]
Addtransactionobserver: Self];
Add this code to implement the transactionobserver function. The corresponding methods can be added later.
Paymentqueue: start
Step 6: The introduction below is not limited to writing a class, but to following the purchase process. Suppose we have compiled a class for In-APP purchase, and then we want to purchase it. The first step is request products.
_ Productrequest = [[skproductsrequestalloc]
Initwithproductidentifiers: _ productidentifiers];
_ Productrequest. Delegate = self;
[_ Productrequest
Start];
A complete request is shown above. For productsidentifiers, this is a set, where a set is created to add each productidentifer.
Nsset * productidentifiers = [nssetsetwithobjects:
K_camera_angle_mode,
K_slope_angle_mode,
K_dihedral_angle_mode,
K_line_plane_angle_mode,
Nil];
Then the methods of the Request delegate
-(Void) productsrequest :( skproductsrequest *) Request didreceiveresponse :( skproductsresponse
*) Response
{
Nsarray * skproducts = response. Products;
// Process ....
}
-(Void) Request :( skrequest *) Request didfailwitherror :( nserror
*) Error
{
// Process ....
}
If the request is successful, you can obtain the product information of the skproduct object in products, an nsarray. Product Information can be found easily by name, price, and so on. These items are only useful for displaying information, and are not required for purchase. You only need to use skproduct.
Step 7: Purchase
Skpayment * payment = [skpaymentpaymentwithproduct: product];
[[Skpaymentqueuedefaquequeue]
Addpayment: payment];
The Code is as above. Then, connect to the app store. Mainly refer to the following
# 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 we will talk about the third Transaction Status restore and restore. This is the case. If some users purchase a product using an account on the iPhone, do they need to purchase the app again after downloading it on the iPad? No. Use restore to check the purchase records of your account in the app store. If a purchase record exists, you do not need to purchase it again and directly restoretransaction.
The next step is to process them separately based on the purchase status.
- (void) completeTransaction: (SKPaymentTransaction *)transaction{
// Your application should implement these two methods. [self recordTransaction:transaction]; [self provideContent:transaction.payment.productIdentifier];
// Remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];}
Generally, we can use nsuserdefaults to record transactions.
Pay attention to the finishtransaction line of code in the program, so that transactionobserver no longer monitors this transaction.
In other States, the restore and failed operations are similar. These codes are included in the development documentation.
Of course, there is also a method for restore to note
-(Void) paymentqueue :( skpaymentqueue *) queue restorecompletedtransactionsfailedwitherror :( nserror
*) Error
If the restore fails, a prompt is displayed.
Step 8: attach
During the entire purchase process, we usually need to give the user a prompt, such as waiting, such as connecting, such as the transaction has been completed. To implement these functions, you should use notification, add postnotification to the previous transaction, and then process notification effectively. This article only describes in-APP purchase, but does not introduce notification programming.
Basically, you can purchase the entire application through the above steps. Of course, in terms of security, Apple will send verification information after the transaction is completed, and send verification information to the App Store to determine whether the transaction is from the App Store, so as to confirm the validity of the transaction.
In this regard, Apple has a code package that provides validationcontroller for convenient verification.