In-IOS purchase

Source: Internet
Author: User

The iOS implementation code is as follows ():

#import "ViewController.h"

#import <StoreKit/StoreKit.h>

@interface Viewcontroller () <skproductsrequestdelegate,skpaymenttransactionobserver >

Record Product List

@property(nonatomic,strong)Nsarray*allproducts;

@end

@implementation Viewcontroller

-(void) viewdidload {

[Super viewdidload];

//1 Request a list of items that can be sold

// prepare a list of items to be sold

nsstring *path=[[nsbundle mainbundle]pathforresource:@ "Products.json" ofType:nil];

nsdata *data=[nsdata datawithcontentsoffile:p ath];

nsarray *arraylist=[nsjsonserialization jsonobjectwithdata:d ata options: 0 error:NULL];

// Product List

nsarray *arrayidlist=[arraylist valueforkeypath:@ "ProductId"];

nsset *set=[nsset setwitharray: arrayidlist];

// Create a Request object

skproductsrequest*request=[[skproductsrequest alloc] Initwithproductidentifiers: set];

// set up proxy

Request. delegate=self;

// start request

[Request start];

// Create listener to listen for trading status of trading objects in the trading queue

[[skpaymentqueue defaultqueue]addtransactionobserver:self];

}

Proxy method , which returns a list of data for the requested object

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

{

for (skproduct*product in response. Products) {

// object contains all information about the Product object

}

self. Allproducts=response. Products;

// Refresh table

[self. TableView reloaddata];

}

2. Show available items for sale

-(Nsinteger) TableView: (uitableview *) TableView numberofrowsinsection: (nsinteger) section

{

return self . Allproducts. Count;

}

-(uitableviewcell*) TableView: (uitableview *) TableView Cellforrowatindexpath: ( Nsindexpath *) indexpath

{

static nsstring *id=@ "cell";

UITableViewCell *cell=[tableview dequeuereusablecellwithidentifier: ID];

if (cell==nil) {

cell=[[UITableViewCell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier: ID];

}

skproduct *product=self. Allproducts[Indexpath. Row];

Cell. Textlabel. Text=product. Localizedtitle;

Cell. Detailtextlabel. Text=[nsstring stringwithformat:@ "%@ circle ", product. Price];

return cell;

}

3 views Buy

-(void) TableView: (uitableview *) TableView Didselectrowatindexpath: (nsindexpath *) Indexpath

{

//4 Invoicing

skproduct *pro=self. Allproducts[Indexpath. Row];

skpayment *payment=[skpayment paymentwithproduct:p ro];

//5, Create a Trade object , add to the trading queue

//nsstring *str=[nsbundle mainbundle]pathforresource:@ "blue.bundle/blue/" OFTYPE:NIL;

[[skpaymentqueue defaultqueue]addpayment:p ayment];

}

when you listen to changes in the trading status in the trading queue, you call

-(void) Paymentqueue: (skpaymentqueue *) queue updatedtransactions: (nsarray *) Transactions

{

// non-consumable items can be recovered for purchase

/*

Skpaymenttransactionstatepurchasing, the transaction is being added to the trading queue

skpaymenttransactionstatepurchased,// trading is already in the queue , the user has paid , the client needs to complete the transaction

Skpaymenttransactionstatefailed,//has not been added to the queue to cancel or fail

skpaymenttransactionstaterestored,// transactions are resumed for purchase and clients need to complete the transaction

Skpaymenttransactionstatedeferred Ns_enum_available_ios (8_0), trading in the queue , trading status is not determined dependent on other parameters involved

*/

//7. If the transaction successfully provides the product of the transaction

for(skpaymenttransaction *t in transactions)

{

if (t.transactionstate= =skpaymenttransactionstatepurchased) {

// services provided

// close trading

[[skpaymentqueue defaultqueue]finishtransaction: t];

}Else if(t.transactionstate= =skpaymenttransactionstaterestored)

{

// Restore purchase Success

// close trading

[[skpaymentqueue defaultqueue]finishtransaction: t];

}

}

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

[[skpaymentqueue defaultqueue]removetransactionobserver:self];

}

In-IOS 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.