標籤:
0:做內購需要添加系統架構storeKit.frameWork
1:登陸蘋果開發人員帳號中去建立商品,即告訴蘋果商場自己都賣哪些東西:
@interface ProductModel : NSObject
/**產品名稱*/
@property (nonatomic, copy) NSString *name;
/**產品id*/
@property (nonatomic, copy) NSString *productID;
2:從伺服器中取出商品,(封裝:資料模型和請求模型)
@implementation AWDataTool
+(void)getProduct:(ResultBlock)resultBlock
{
ProductModel *model = [[ProductModel alloc] init];
model.name = @"大神跑鞋";
model.productID = @"dashenpaoxie";
resultBlock(@[model]);
}
3:將商品發送到蘋果伺服器,請求可以銷售的商品;
//伺服器中取出商品
[AWDataTool getProduct:^(NSArray<ProductModel *> *goods) {
//取出商品ID
NSArray *product = [goods valueForKey:@"productID"];
//將商品發送到蘋果伺服器,請求可以銷售的商品
NSSet *set = [NSSet setWithArray:product];
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
//設定代理從代理中擷取哪些商品可以銷售
request.delegate = self;
[request start];
}];
#pragma mark - SKProductsRequestDelegate
/**
* 當請求到結果時會調用該方法
*
* @param request 請求
* @param response 響應
*/
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
self.productList = response.products;
}
-----------------------------------uitableViewController所有代碼如下-----------------------------------------
//
// ViewController.m
// 大神一期內購測試
//
// Created by apple on 16/4/5.
// Copyright © 2016年 apple. All rights reserved.
//
#import "ViewController.h"
#import <StoreKit/StoreKit.h>
#import "XMGDataTool.h"
#import "XMGProduct.h"
@interface ViewController () <SKProductsRequestDelegate>
/** 能銷售的商品列表 */
@property (strong, nonatomic) NSArray <SKProduct *> *products;
@end
@implementation ViewController
- (void)setProducts:(NSArray<SKProduct *> *)products {
_products = products;
[self.tableView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 1.從伺服器擷取商品
[XMGDataTool getProducts:^(NSArray<XMGProduct *> *goods) {
// 取出商品標示
NSArray *identifiers = [goods valueForKeyPath:@"proId"];
// 將商品標示發送到蘋果伺服器,請求可以銷售的商品
NSSet *set = [[NSSet alloc] initWithArray:identifiers];
// 請求對象
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
// 設定代理,從代理中擷取哪些商品可以銷售
request.delegate = self;
// 開始請求
[request start];
}];
}
#pragma mark - SKProductsRequestDelegate
/**
* 當請求到結果時會調用該方法
*
* @param request 請求
* @param response 響應
*/
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSLog(@"%@",response.products);
self.products = response.products;
}
#pragma mark - 資料來源
- (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.取出模型
SKProduct *product = self.products[indexPath.row];
// 2.設定資料
cell.textLabel.text = product.localizedTitle;
cell.detailTextLabel.text = product.localizedDescription;
return cell;
}
**
* 當交易狀態發生改變時會調用該方法
*
* @param queue 交易隊列
* @param transactions 交易商品數組
*/
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions NS_AVAILABLE_IOS(3_0) {
//SKPaymentTransactionStatePurchasing, 正在支付
//SKPaymentTransactionStatePurchased, 支付成功
//SKPaymentTransactionStateFailed, 支付失敗
//SKPaymentTransactionStateRestored, 恢複購買
//SKPaymentTransactionStateDeferred 延遲購買
[transactions enumerateObjectsUsingBlock:^(SKPaymentTransaction * _Nonnull transaction, NSUInteger idx, BOOL * _Nonnull stop) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
NSLog(@"正在支付");
break;
case SKPaymentTransactionStatePurchased:
// 當支付完成時,一定要將該訂單,從支付隊列中移除
[queue finishTransaction:transaction];
NSLog(@"支付成功");
break;
case SKPaymentTransactionStateFailed:
// 當支付失敗時,一定要將該訂單,從支付隊列中移除
[queue finishTransaction:transaction];
NSLog(@"支付失敗");
break;
case SKPaymentTransactionStateRestored:
NSLog(@"恢複購買");
// 當恢複時,一定要將該訂單,從支付隊列中移除
[queue finishTransaction:transaction];
break;
case SKPaymentTransactionStateDeferred:
NSLog(@"延遲購買");
break;
default:
break;
}
}];
}
#pragma mark - 資料來源
- (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.取出模型
SKProduct *product = self.products[indexPath.row];
// 2.設定資料
cell.textLabel.text = product.localizedTitle;
cell.detailTextLabel.text = product.localizedDescription;
return cell;
}
#pragma mark - 代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 1.取出選中的商品
SKProduct *product = self.products[indexPath.row];
// 2.根據商品開一個小票
SKPayment *payment = [SKPayment paymentWithProduct:product];
// 3.將小票添加到隊列中
SKPaymentQueue *queue = [SKPaymentQueue defaultQueue];
[queue addPayment:payment];
// 4.監聽交易狀態
[queue addTransactionObserver:self];
}
@end
iOS內購步驟總結