iOS Internal purchase Steps summary

Source: Internet
Author: User

0: Do in-purchase need to add system framework Storekit.framework

1: Log in to the Apple developer account to create a product that tells the Apple Store what it sells:

@interface Productmodel:nsobject

/** Product Name */

@property (nonatomic, copy) NSString *name;

/** Product id*/

@property (nonatomic, copy) NSString *productid;

2: Remove the product from the server (package: Data model and request model)

@implementation Awdatatool

+ (void) GetProduct: (Resultblock) Resultblock

{

ProductModel *model = [[ProductModel alloc] init];

Model.name = @ "Great God running shoes";

Model.productid = @ "Dashenpaoxie";

Resultblock (@[model]);

}

3: Send the product to the Apple server, request the goods that can be sold;

Remove items from the server

[Awdatatool getproduct:^ (Nsarray<productmodel *> *goods) {

Remove Item ID

Nsarray *product = [Goods valueforkey:@ "ProductID"];

Send a product to an Apple server and request a product that can be sold

Nsset *set = [Nsset setwitharray:product];

skproductsrequest *request = [[Skproductsrequest alloc] initwithproductidentifiers:set];

Set up what products the agent gets from the agent to sell

Request.delegate = self;

[Request start];

}];

#pragma mark-skproductsrequestdelegate

/**

* This method is called when the result is requested

*

* @param request

* @param response response

*/

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

{

Self.productlist = response.products;

}

-----------------------------------Uitableviewcontroller All code is as follows-----------------------------------------

//

Viewcontroller.m

Big God in one-time purchase test

//

Created by Apple on 16/4/5.

COPYRIGHT©2016 year Apple. All rights reserved.

//

#import "ViewController.h"

#import <StoreKit/StoreKit.h>

#import "XMGDataTool.h"

#import "XMGProduct.h"

@interface Viewcontroller () <SKProductsRequestDelegate>

List of items/** can sell * *

@property (Strong, nonatomic) Nsarray <skproduct *> *products;

@end

@implementation Viewcontroller

-(void) Setproducts: (nsarray<skproduct *> *) Products {

_products = Products;

[Self.tableview Reloaddata];

}

-(void) Viewdidload {

[Super Viewdidload];

1. Get the product from the server

[Xmgdatatool getproducts:^ (nsarray<xmgproduct *> *goods) {

Take out the product mark

Nsarray *identifiers = [Goods valueforkeypath:@ "proid"];

Send a product label to an Apple server and request a product that can be sold

Nsset *set = [[Nsset alloc] initwitharray:identifiers];

Request Object

Skproductsrequest *request = [[Skproductsrequest alloc] initwithproductidentifiers:set];

Set up the agent to get what items can be sold from the agent

Request.delegate = self;

Start request

[Request start];

}];

}

#pragma mark-skproductsrequestdelegate

/**

* This method is called when the result is requested

*

* @param request

* @param response response

*/

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

NSLog (@ "%@", response.products);

Self.products = response.products;

}

#pragma mark-Data source

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {

return self.products.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];

}

1. Remove the Model

Skproduct *product = Self.products[indexpath.row];

2. Setting up the data

Cell.textLabel.text = Product.localizedtitle;

Cell.detailTextLabel.text = product.localizeddescription;

return cell;

}

**

* This method will be called when the trading status has changed

*

* @param queue Transaction

* @param transactions Trading Product Array

*/

-(void) Paymentqueue: (Skpaymentqueue *) queue updatedtransactions: (nsarray<skpaymenttransaction *> *) Transactions Ns_available_ios (3_0) {

Skpaymenttransactionstatepurchasing, paying now.

skpaymenttransactionstatepurchased, Payment success

Skpaymenttransactionstatefailed, Payment failed

skpaymenttransactionstaterestored, restore purchase

Skpaymenttransactionstatedeferred Late Purchase

[Transactions enumerateobjectsusingblock:^ (Skpaymenttransaction * _nonnull transaction, Nsuinteger idx, BOOL * _Nonnull Stop) {

Switch (transaction.transactionstate) {

Case skpaymenttransactionstatepurchasing:

NSLog (@ "paying");

Break

Case skpaymenttransactionstatepurchased:

When payment is complete, be sure to remove the order from the payment queue

[Queue finishtransaction:transaction];

NSLog (@ "payment success");

Break

Case skpaymenttransactionstatefailed:

When the payment fails, be sure to remove the order from the payment queue

[Queue finishtransaction:transaction];

NSLog (@ "payment failed");

Break

Case skpaymenttransactionstaterestored:

NSLog (@ "restore purchase");

When recovering, be sure to remove the order from the payment queue

[Queue finishtransaction:transaction];

Break

Case skpaymenttransactionstatedeferred:

NSLog (@ "late purchase");

Break

Default

Break

}

}];

}

#pragma mark-Data source

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {

return self.products.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];

}

1. Remove the Model

Skproduct *product = Self.products[indexpath.row];

2. Setting up the data

Cell.textLabel.text = Product.localizedtitle;

Cell.detailTextLabel.text = product.localizeddescription;

return cell;

}

#pragma mark-Agent

-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {

1. Remove the selected item

Skproduct *product = Self.products[indexpath.row];

2. According to the commodity to open a small ticket

Skpayment *payment = [Skpayment paymentwithproduct:product];

3. Add a small ticket to the queue

Skpaymentqueue *queue = [Skpaymentqueue defaultqueue];

[Queue addpayment:payment];

4. Monitor transaction status

[Queue addtransactionobserver:self];

}

@end

iOS Internal purchase Steps summary

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.