In-Apple purchases include 4 types: 1. Consumption, 2. Non-expendable; 3. Auto-renew subscription; 4. Non-renewal subscription.
First of all, there is a itunesconnect on the agreement, tax and Banking module has a bank card binding while complying with the various Apple Protocol Development Account, (how to apply for filling online Baidu a bunch)
Then remember to add the protocol before the Code
<SKPaymentTransactionObserver,SKProductsRequestDelegate,SKRequestDelegate>
Purchase click-(void) paybtnclick{//Purchase if ([Skpaymentqueue canmakepayments]) {[Self Requestproductdata:produ CTID]; } else {NSLog (@ "user prohibits in-app pay purchases.");}} Restore click-(void) restoreclick{[[Skpaymentqueue Defaultqueue] restorecompletedtransactions];} Request goods-(void) Requestproductdata: (NSString *) type{NSLog (@ "----------request the corresponding product information---------"); Nsarray *product = [[Nsarray alloc] initwithobjects:type, nil]; Nsset *nsset = [Nsset setwitharray:product]; Skproductsrequest *request = [[Skproductsrequest alloc] initwithproductidentifiers:nsset]; Request.delegate = self; [Request start];} -(void) Productsrequest: (skproductsrequest *) Request Didreceiveresponse: (Skproductsresponse *) response{NSLog (@ "--- -------receive the Product feedback message-------------"); Nsarray *product = response.products; if (Product.count = = 0) {NSLog (@ "------No merchandise, the number of items is%ld--------", product.count); Return } skproduct *p = nil; For (skproduct *pro in product) {if ([Pro.productidentifier Isequaltostring:productid]) {p = Pro; }} skpayment *payment = [Skpayment paymentwithproduct:p]; NSLog (@ "Send purchase Request"); [[Skpaymentqueue Defaultqueue] addpayment:payment];} -(void) Paymentqueue: (Skpaymentqueue *) queue updatedtransactions: (Nsarray *) transactions//trade Result {NSLog (@) Paymentqueue "); For (Skpaymenttransaction *transaction in transactions) {switch (transaction.transactionstate) { Case SKPAYMENTTRANSACTIONSTATERESTORED://has purchased the product and restored the product [self restoretransaction:transaction]; NSLog (@ "-----have already purchased the product to recover the goods--------"); [[Xtool Shareapphandle]showwait:no]; Break Case skpaymenttransactionstatepurchased://transaction complete [self completetransaction:transaction]; NSLog (@ "-----transaction completed--------"); Break Case skpaymenttransactionstatefailed://transaction failed [self failedtransaction:Transaction]; NSLog (@ "-----transaction failed--------"); [[Xtool Shareapphandle]showpaixuwait:no]; Break Case skpaymenttransactionstatepurchasing://Item added to list NSLog (@ "-----Item added to list--------"); Break Case Skpaymenttransactionstatedeferred:break; Default:break; }}}//Complete Recovery-(void) paymentqueuerestorecompletedtransactionsfinished: (Skpaymentqueue *) queue{NSLog (@ " Paymentqueuerestorecompletedtransactionsfinished "); NSLog (@ "%@", [queue description]); NSLog (@ "-----Complete recovery--------");} -(void) Restoretransaction: (skpaymenttransaction *) transaction{NSLog (@ "Transaction recovery processing"); NSLog (@ "-----Restore success--------"); NSData *iapdata = Transaction.transactionreceipt; Your application should implement these, methods. NSString *product = Transaction.payment.productIdentifier; if ([Product length] > 0) {nsarray *TT = [Product Componentsseparatedbystring:@ "."]; NSString *bookid = [TT Lastobject]; if ([bookid length] > 0) {[Self recordtransaction:bookid]; [Self providecontent:bookid]; }} nsstring *results = [[nsstring alloc]initwithbytes:[iapdata bytes] length:[iapdata length] encoding:nsutf8string Encoding]; {//Recover transaction information sent to server code} [[Skpaymentqueue Defaultqueue] finishtransaction:transaction];} Record transaction-(void) Recordtransaction: (NSString *) product{NSLog (@ "-----record transaction--------");} Processing downloads-(void) Providecontent: (NSString *) product{NSLog (@ "-----download--------");} -(void) Purchasedtransaction: (skpaymenttransaction *) transaction{NSLog (@ "-----purchasedtransaction----"); Nsarray *transactions =[[nsarray alloc] initwithobjects:transaction, nil]; [Self paymentqueue:[skpaymentqueue defaultqueue] updatedtransactions:transactions]; [[Skpaymentqueue Defaultqueue] finishtransaction:transaction];} -(void) Completetransaction: (Skpaymenttransaction *)transaction{NSLog (@ "-----purchase success--------"); NSData *iapdata = Transaction.transactionreceipt; Your application should implement these, methods. NSString *product = Transaction.payment.productIdentifier; if ([Product length] > 0) {nsarray *TT = [Product componentsseparatedbystring:@ "]; NSString *bookid = [TT Lastobject]; if ([bookid length] > 0) {[Self recordtransaction:bookid]; [Self providecontent:bookid]; }} nsstring *results = [[nsstring alloc]initwithbytes:[iapdata bytes] length:[iapdata length] encoding:nsutf8string Encoding]; NSLog (@ "%@", results); {//Purchase information sent to server} [[Skpaymentqueue Defaultqueue] finishtransaction:transaction];} -(void) Failedtransaction: (skpaymenttransaction *) transaction{if (transaction.error.code! = skerrorpaymentcancelled {} [[Skpaymentqueue defaultqueue] finishtransaction:transaction];} -(void) Request: (Skrequest *) Request DIDFAILWITherror: (Nserror *) error{NSLog (@ "purchase Failed");} -(void) Paymentqueue: (Skpaymentqueue *) paymentqueue restorecompletedtransactionsfailedwitherror: (NSError *) error{ NSLog (@ "-------paymentqueue----");} -(void) connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) response{NSLog (@ "%ld", (long) [ (Nshttpurlresponse *) response statusCode]); Switch ([(nshttpurlresponse *) Response StatusCode]) {case 200:case 206:break; Case 304:break; Case 400:break; Case 404:break; Case 416:break; Case 403:break; Case 401:case 500:break; Default:break; }}-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error {NSLog (@ "Test");} -(void) Requestdidfinish: (Skrequest *) request{NSLog (@ "-----feedback end-------");} -(BOOL) Verify: (Skpaymenttransaction *) transaction{return YES;
The basic code required for purchase is as above;
1. Consumable type
The macro definition (or from the server to see the demand) gets the ID of the purchased product (the product ID requested on itunesconnect), and the code in the above
ProductID Replace with the ID of the purchased product
The return message is then sent to the server for processing
Then the server side and Apple make a purchase verification, confirm the purchase success, return to the client to purchase the product successfully
The client displays the appropriate purchase content.
2. Non-consumable type
With the consumption type, the only difference is to repeat the purchase, Apple will prompt you can not repeat the purchase. It has nothing to do with the client or the server.
3. Automatic subscription Renewal
Purchase process with consumption type, more than a recovery purchase, while the display must be reminded that the user is currently subscribed to automatic renewal of the text description, or the Apple audit may be rejected
When the process is resumed, the user clicks Restore purchase and executes
Restoreclick method
If the recovery process is normal (not normal, you need to check the contents of the product request, sandbox test account problems, etc. before the steps are wrong)
Restoretransaction method
After acquiring the user's purchase information, the client transmits to the server, the server side and Apple perform data validation, and the client displays the product content that the client should restore after verifying the error.
4. Non-renewal subscriptions
Purchase process and consumption type;
The recovery process needs to go to the page and click Restore to execute
Restoreclick method
At the same time execute the following code to obtain the user purchase record data, continue to obtain until the user or cancel recovery.
Nsurlrequest *urlrequest = [nsurlrequest requestwithurl:[[nsbundle mainbundle] appstorereceipturl]]; Nserror *error = nil; NSData *iapdata = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:nil error:&error]; NSString *transactionreceiptstring = [Iapdata base64encodedstringwithoptions: Nsdatabase64encodingendlinewithlinefeed];
If the user has entered the AppleID, and the recovery operation is done;
At this point, the acquired user purchase record and the current recovery product ID are sent to the server, the server and Apple for purchase confirmation. If the product has already been purchased, the recovery success information is returned to the client. The client shows what needs to be restored to the presentation.
Apple in-purchase notes