码迷,mamicode.com
首页 > 其他好文 > 详细

内购和广告

时间:2015-07-04 22:12:22      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

 
 

内购原理(类似商场交易):
     技术分享
 
内购实现步骤:
     导入StoreKit框架
1.用要销售的商品列表 给苹果
1.1.创建一个产品请求  用来请求可销售的商品列表
//开始请求
2.返回一个可以销售的商品列表
//3.展示可销售的商品列表
4.用户点击购买 -> 生成一个交易
5.把交易添加到交易队列(交易对象需要监听者来监听交易状态)
6.添加一个监听者 监听交易的状态
    [[SKPaymentQueue defaultQueue]addTransactionObserver:self];
7.交易完成  - >  提供增值服务
 

内购示例:

#import "ViewController.h"
#import <StoreKit/StoreKit.h>
@interface ViewController ()<SKProductsRequestDelegate,SKPaymentTransactionObserver>
@property (nonatomic, strong) NSArray *allProducts;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //1.用要销售的商品列表 给苹果
   
    NSString *path = [[NSBundle mainBundle]pathForResource:@"products.json" ofType:nil];
   
    NSData *data = [NSData dataWithContentsOfFile:path];
   
    NSArray *allData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
  
   
    NSArray *avProducts = [allData valueForKeyPath:@"productId"];
     NSLog(@"%@",avProducts);
    NSSet *set = [NSSet setWithArray:avProducts];
    //1.1.创建一个产品请求  用来请求可销售的商品列表
    SKProductsRequest *request = [[SKProductsRequest alloc]initWithProductIdentifiers:set];
   
    request.delegate = self;
   
    //开始请求
    [request start];

    //6.添加一个监听者
    [[SKPaymentQueue defaultQueue]addTransactionObserver:self];
   }
#pragma mark -<SKProductsRequestDelegate>
/**
 *  返回商品列表的时候就会调用
 *
 *  @param request  请求对象
 *  @param response 返回结果
 */

//2.返回一个可以销售的商品列表

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    for (SKProduct *product in response.products) {
       
        NSLog(@"%@  %@  %@ ",product.localizedTitle,product.localizedDescription,product.price);
       
       
    }
    self.allProducts = response.products;
    //更新界面
    [self.tableView reloadData];
}
/**
 *  //3.展示可销售的商品列表
 */
- (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:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
   
   
    SKProduct *product = self.allProducts[indexPath.row];
   
   
    cell.textLabel.text = product.localizedTitle;
   
   
    return cell;
}
//4.用户点击购买 -> 生成一个交易

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SKProduct * p = self.allProducts[indexPath.row];
   
   
    //5.把交易添加到交易队列(交易对象需要监听者来监听交易状态)
   
    SKPayment *payment = [SKPayment paymentWithProduct:p];
   
    //添加交易(小票)到交易队列
    [[SKPaymentQueue defaultQueue]addPayment:payment];
   
}

#pragma mark- SKPaymentTransactionObserver
/**
 *  交易队列中交易状态改变的时候就会调用
 */
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    /*
     SKPaymentTransactionStatePurchasing,    // 交易正在被添加到交易队列
     SKPaymentTransactionStatePurchased,     // 交易已经在队列,客户端需要完成交易
     SKPaymentTransactionStateFailed,        // 失败了  还没有添加到交易队列
     SKPaymentTransactionStateRestored,      // 交易从用户的购买记录中恢复 客户端需要完成交易
     SKPaymentTransactionStateDeferred NS_ENUM_AVAILABLE_IOS(8_0),   交易在队列, 但是其最终状态还需要别的参与
     */
    //7.交易完成  - >  提供增值服务
    for (SKPaymentTransaction *paymentTransaction in transactions) {
    
        if (paymentTransaction.transactionState == SKPaymentTransactionStatePurchased) {
            NSLog(@"购买成功 提供增值服务");
           
            // 因为交易已经完成  手动结束交易
            [[SKPaymentQueue defaultQueue]finishTransaction:paymentTransaction];
           
        }
       
        if (paymentTransaction.transactionState == SKPaymentTransactionStateRestored) {
            NSLog(@"恢复购买成功!");
            [[SKPaymentQueue defaultQueue]finishTransaction:paymentTransaction];
        }
       
    }
   
}

- (IBAction)restoreP:(id)sender {
   
    [[SKPaymentQueue defaultQueue]restoreCompletedTransactions];
   
}
@end
 

广告:
     导入广告的iAd框架
     在view中合适的位置添加一个iAd控件
     设置广告的代理,在代理方法中实现广告功能
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{     
    //广告的约束条件改变  从view的底部弹出
    self.consxin.constant = 50;
    [UIView animateWithDuration:2 animations:^{
        [self.view layoutIfNeeded];
    }];
    NSLog(@"加载完毕");
}

内购和广告

标签:

原文地址:http://www.cnblogs.com/guozhenhu/p/4621309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!