iOS开发iOS面试知识点收集你可不能输!

iOS内购(iap)

2015-10-06  本文已影响8137人  Little_Dragon
通过苹果应用程序商店有三种主要赚钱的方式:

1.直接收费(我想天朝的大多数人是不会购买的)
2.广告 iAd Framework
3.内购:应用程序本身的增值产品,(比如什么欢乐豆之类的)
一般式37开, 苹果3,开发商7

内购的产品分类:

1>非消耗品(Nonconsumable)一旦购买,终身拥有(终身会员)
2>消费品(Consumable),买了就用,用了就没有了(欢乐豆)
剩下三种不常用:(中国用不上 iBooks)
3>免费订阅
4>自动续费订阅
5>非自动续费订阅

内购流程, 苹果官方说明:


Snip20151006_3.png Snip20151006_4.png

添加StoreKit框架,进行内购流程的书写:

#import "ViewController.h"
#import <StoreKit/StoreKit.h>

@interface ViewController () <SKProductsRequestDelegate, SKPaymentTransactionObserver>

/** 所有的产品 */
@property (nonatomic, strong) NSArray *products;

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 通过观察者监听交易状态
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 向苹果服务器请求可卖的商品
    [self requestProducts];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"恢复" style:UIBarButtonItemStyleDone target:self action:@selector(restore)];
}

/**
 *  请求可卖商品
 */
- (void)requestProducts
{
    // 1.请求所有的商品
    NSString *productFilePath = [[NSBundle mainBundle] pathForResource:@"iapdemo.plist" ofType:nil];
    NSArray *products = [NSArray arrayWithContentsOfFile:productFilePath];
    
    // 2.获取所有的productid
    NSArray *productIds = [products valueForKeyPath:@"productId"];
    
    // 3.获取productid的set(集合中)
    NSSet *set = [NSSet setWithArray:productIds];
    
    // 4.向苹果发送请求,请求可卖商品
    SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
    request.delegate = self;
    [request start];
}

/**
 *  当请求到可卖商品的结果会执行该方法
 *
 *  @param response response中存储了可卖商品的结果
 */
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    /*
    for (SKProduct *product in response.products) {
        NSLog(@"价格:%@", product.price);
        NSLog(@"标题:%@", product.localizedTitle);
        NSLog(@"秒速:%@", product.localizedDescription);
        NSLog(@"productid:%@", product.productIdentifier);
    }
     */
    // 1.存储所有的数据
    self.products = response.products;
    self.products = [self.products sortedArrayWithOptions:NSSortConcurrent usingComparator:^NSComparisonResult(SKProduct *obj1, SKProduct *obj2) {
        return [obj1.price compare:obj2.price];
    }];
    
    // 2.刷新表格
    [self.tableView reloadData];
}

#pragma mark - tableView的数据源和代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.products.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"ProductCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    // 1.取出模型
    SKProduct *product = self.products[indexPath.row];
    
    // 2.给cell设置数据
    cell.textLabel.text = product.localizedTitle;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"价格:%@", product.price];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.取出模型
    SKProduct *product = self.products[indexPath.row];
    
    // 2.购买商品
    [self buyProduct:product];
}

#pragma mark - 购买商品
- (void)buyProduct:(SKProduct *)product
{
    // 1.创建票据
    SKPayment *payment = [SKPayment paymentWithProduct:product];
    
    // 2.将票据加入到交易队列中
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

#pragma mark - 实现观察者回调的方法
/**
 *  当交易队列中的交易状态发生改变的时候会执行该方法
 *
 *  @param transactions 数组中存放了所有的交易
 */
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    /*
     SKPaymentTransactionStatePurchasing, 正在购买
     SKPaymentTransactionStatePurchased, 购买完成(销毁交易)
     SKPaymentTransactionStateFailed, 购买失败(销毁交易)
     SKPaymentTransactionStateRestored, 恢复购买(销毁交易)
     SKPaymentTransactionStateDeferred 最终状态未确定
     */
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchasing:
                NSLog(@"用户正在购买");
                break;
                
            case SKPaymentTransactionStatePurchased:
                NSLog(@"购买成功");
                [queue finishTransaction:transaction];
                break;
                
            case SKPaymentTransactionStateFailed:
                NSLog(@"购买失败");
                [queue finishTransaction:transaction];
                break;
                
            case SKPaymentTransactionStateRestored:
                NSLog(@"恢复购买");
                [queue finishTransaction:transaction];
                break;
                
            case SKPaymentTransactionStateDeferred:
                NSLog(@"最终状态未确定");
                break;
                
            default:
                break;
        }
    }
}
// 恢复购买
- (void)restore
{
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
广告: 苹果自己搞的内容

1.添加iAd.framework
2.添加ADBannerView视图,并设置代理方法
3.广告条加载完成之前最好隐藏

-
(void)bannerViewDidLoadAd:(ADBannerView
*)banner {

    self.bannerBottomConstraint.constant
= 20.0;    

    [UIView
animateWithDuration:0.5
animations:^{

        [self.view
layoutIfNeeded];

    }];   

    NSLog(@"加载广告成功");

}

-
(void)bannerView:(ADBannerView
*)banner didFailToReceiveAdWithError:(NSError
*)error {

    NSLog(@"加载广告失败
%@",
error);

}
上一篇 下一篇

猜你喜欢

热点阅读