iOS in-app payment (IAP) Development Steps List
The first two days, along with the service colleagues, completed the development of in-app billing (IAP, in app purchase). A wide range of steps to organize the list of development steps below. Because it is just a list of steps, so it does not contain detailed instructions for the tutorial, you need to look at the tutorial novice, you can see I attached to the last some reference links.
Configure Developer.apple.com
Log in to Developer.apple.com, and then perform the following steps:
- Create an app ID without wildcards for your app
- Use the app ID to generate and install the appropriate provisioning profile.
Configure itunes Connect
Log in to itunes Connet, and then perform the following steps:
- Create a new app with the app ID.
-
In this app, create in-app paid items, choose a pay type, usually optional consumable or permanently active (non-consumable) 2, then set the price and Product ID, and the purchase description and the product The ID is to be remembered, and it needs to be developed later. As shown in the following:
-
Add a test user to pay for the sandbox, as shown in. Note that Apple's password requirements for the test user and the official account must be at least 8 digits and include both numbers and uppercase and lowercase letters:
-
Fill in the relevant tax, bank, contact information. As shown in the following:
Development work (iOS side)
1. Introduction of Storekit.framework and #import <StoreKit/StoreKit.h> in engineering
2. Get a list of all paid product IDs. This can be stored locally on a constant, or returned by its own server.
3. Create an interface to show all in-app paid items. The price and introductory information for these in-app paid items can be returned by your own server. However, if it is a standalone game app or tool class app without a server, it can be obtained by querying the App Store. When I was testing, I found that it was very slow to query the App Store, which usually takes 2-3 seconds, so it is not recommended to do so, it is best to get a server of your own.
4, when the user clicked on an IAP item, we first check whether the user allows in-app billing, if not allowed to do the following steps. The code is as follows:
123456 |
if ([SKPaymentQueue canMakePayments]) { // 执行下面提到的第5步: [self getProductInfo];} else { NSLog(@"失败,用户禁止应用内付费购买.");}
|
5, we first through the IAP ProductID to AppStore query, get skpayment instance, and then through the Skpaymentqueue Addpayment method to initiate a purchase operation.
123456789ten |
The following ProductID should be a pre-existing paid item added in Itunesconnect. Otherwise, the query will fail.-(void)Getproductinfo{ Nsset*Set=[NssetSetwitharray:@[@ "ProductId"]]; Skproductsrequest*Request=[[SkproductsrequestAlloc]Initwithproductidentifiers:Set]; Request.Delegate=Self; [RequestStart];}callback function for the above query-(void)Productsrequest:(Skproductsrequest*)RequestDidreceiveresponse:(Skproductsresponse*)Response{ Nsarray*MyProduct=Response.Products; If(MyProduct.Count==0) { nslog Span class= "s" >@ "unable to obtain product information, purchase failed. "); return; } skpayment * payment = [skpayment paymentwithproduct: myproduct[0]]; [[skpaymentqueue defaultqueue] Addpayment:payment]; /span>
|
6, in the Viewdidload method, the purchase page is set to purchase the observer.
123456789 |
-(void)Viewdidload{ [SuperViewdidload]; //monitoring purchase results [[skpaymentqueue Defaultqueue] addtransactionobserver:self];< Span class= "line" >}- ( Span class= "kt" >void) viewdidunload { [super viewdidunload]; [[skpaymentqueue defaultqueue] Removetransactionobserver:self]; /span>
|
7, when the user buys the operation has the result, will trigger the following callback function, the corresponding processing can.
123456789101112131415161718192021st2223the40 of the above-the-- The all-in-a- |
-(void)Paymentqueue:(Skpaymentqueue*)QueueUpdatedtransactions:(Nsarray*)Transactions{ For(Skpaymenttransaction*TransactionInchTransactions) { Switch(Transaction.Transactionstate) { CaseSkpaymenttransactionstatepurchased:Transaction completion NSLog(@ "Transactionidentifier =%@",Transaction.Transactionidentifier); [SelfCompletetransaction:Transaction]; Break; CaseSkpaymenttransactionstatefailed:Transaction failure [SelfFailedtransaction:Transaction]; Break; CaseSkpaymenttransactionstaterestored:have purchased this product [SelfRestoretransaction:Transaction]; Break; CaseSkpaymenttransactionstatepurchasing:Add items to the list NSLog(@ "Product added into List"); Break; Default: Break; } }}-(void)Completetransaction:(Skpaymenttransaction*)Transaction{ Your application should implement these, methods. NSString*Productidentifier=Transaction.Payment.Productidentifier; NSString*Receipt=[Transaction.TransactionreceiptBase64encodedstring]; If([ProductidentifierLength]>0){ Verify the proof of purchase to your own server } Remove the transaction from the payment queue. [[Skpaymentqueuedefaultqueue finishtransaction: transaction< Span class= "P" >; }- Span class= "P" > (void) failedtransaction: (skpaymenttransaction * transaction {< Span class= "line" > if (Transaction.error.code! = skerrorpaymentcancelled) {NSLog (@ "purchase Failed");} else { NSLog (@ "User cancels the transaction"); } [[Skpaymentqueue defaultqueue] finishtransaction:transaction];} -(void) Restoretransaction: (skpaymenttransaction *) transaction {//for purchased goods, processing the logic to restore purchases [[Skpaymentqueue Defaultqueue] Finishtransaction:transaction];}
|
8, Server Authentication certificate (Optional). If the purchase is successful, we need to send the voucher to the server for verification. Taking into account the network anomalies, the IOS send credential operation should be persisted, if the program exits, crashes or network exceptions, you can resume the retry.
Development work (server side)
The service side of the work is relatively simple, divided into 4 steps:
- Receive proof of purchase from the iOS side.
- Determine if the credential already exists or has been validated, and then store the voucher.
- Send the credential to Apple's server for authentication and return the validation results to the client.
- If necessary, modify the user's corresponding member permissions.
Given the network anomalies, the server's authentication should be a recoverable queue and should be retried if the network fails.
The authentication interface document with Apple is here. In a nutshell, the purchase voucher is encoded with Base64 and then post to the Apple authentication server, and Apple returns the validation results in JSON form.
The verification address for the purchase voucher on the Apple AppStore line is https://buy.itunes.apple.com/verifyReceipt, and the verified address for the test is: https://sandbox.itunes.apple.com/verifyReceipt
Reference links
The following reference links detail the steps to complete in-app paid development:
- Https://developer.apple.com/appstore/in-app-purchase/index.html
- Http://www.himigame.com/iphone-cocos2d/550.html
- Http://www.cocoachina.com/iphonedev/sdk/2011/1028/3435.html
- Http://www.cocoachina.com/newbie/basic/2012/0214/3976.html
iOS in-app payment (IAP) Development steps list