Apple Internal Purchase Demo

Source: Internet
Author: User
Tags uikit

1. Pour: storekit.framework Library

2. Code

1 #import<UIKit/UIKit.h>2 3 //Import Library4 #import<StoreKit/StoreKit.h>5 @interfaceViewcontroller:uiviewcontroller<skproductsrequestdelegate,skpaymenttransactionobserver>6@property (Strong, nonatomic) iboutlet Uitextfield *ProductID;7@property (Strong, nonatomic) iboutlet UIButton *Purchase;8 //xib buy button9-(Ibaction) Purchasefunc: (ID) sender;Ten @end
#import "ViewController.h"/** In the test purchase you enter the purchase user must be the user intelligence under the sandbox technical tester inside the account*/@interfaceViewcontroller () @property (Nonatomic,copy,ReadOnly) NSString *Localizedprice;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload];    [[Skpaymentqueue Defaultqueue] addtransactionobserver:self]; Self.productID.text=@"Com.fadada.grB";//ID of your in-product purchase }//Click-(Ibaction) Purchasefunc: (ID) Sender {NSString*product =Self.productID.text; if([Skpaymentqueue canmakepayments]) {[Self requestproductdata:product]; }Else{NSLog (@"do not allow in-app billing"); }}//Request a product- (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]; }//Receive product return information- (void) Productsrequest: (skproductsrequest *) Request Didreceiveresponse: (Skproductsresponse *) response{NSLog (@"--------------receive a product feedback message---------------------"); Nsarray*product =response.products; if([Product count] = =0) {NSLog (@"--------------No goods------------------"); return; } NSLog (@"productid:%@", response.invalidproductidentifiers); NSLog (@"number of products paid:%ld", [Product Count]); Skproduct*p =Nil;  for(Skproduct *proinchproduct) {NSLog (@"%@", [pro description]); NSLog (@"%@", [pro localizedtitle]); NSLog (@"%@", [pro localizeddescription]); NSLog (@"%@", [pro price]); NSLog (@"%@", [pro productidentifier]); if([Pro.productidentifier IsEqualToString:self.productID.text]) {p=Pro; }} skpayment*payment =[Skpayment paymentwithproduct:p]; NSLog (@"Send Purchase Request"); [[Skpaymentqueue Defaultqueue] addpayment:payment];}//request failed- (voidRequest: (Skrequest *) Request Didfailwitherror: (Nserror *) error{NSLog (@"------------------Error-----------------:%@", error);}- (void) Requestdidfinish: (Skrequest *) request{NSLog (@"------------End of feedback information-----------------");}//Monitor Purchase Results- (void) Paymentqueue: (Skpaymentqueue *) queue updatedtransactions: (Nsarray *) transaction{ for(Skpaymenttransaction *traninchtransaction) {                Switch(tran.transactionstate) { CaseSkpaymenttransactionstatepurchased:nslog (@"Transaction Completion");  Break;  CaseSkpaymenttransactionstatepurchasing:nslog (@"Add items to the list");  Break;  CaseSkpaymenttransactionstaterestored:nslog (@"have purchased a product");  Break;  CaseSkpaymenttransactionstatefailed:nslog (@"Transaction Failure");  Break; default:                 Break; }    }}//End of transaction- (void) Completetransaction: (Skpaymenttransaction *) transaction{NSLog (@"End of transaction"); [[Skpaymentqueue Defaultqueue] finishtransaction:transaction];}- (void) dealloc{[[Skpaymentqueue Defaultqueue] removetransactionobserver:self];}

3.xib

Apple Internal Purchase Demo

Related Article

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.