• ios内购


    1.添加框架,storeKit.framework

      需要真机调试

    /*

     内购五步: 1.请求可销售商品的列表

            2.展示课销售的商品

            3.点击购买

            4.开具小票

            5.创建交易对象并添加到交易队列

            6.创建监听对象,监听交易队列中交易对象的交易状态

            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 *arrayList=[NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

        //产品列表

        NSArray *arrayIdList=[arrayList valueForKeyPath:@"productId"];

        NSSet *set=[NSSet setWithArray:arrayIdList];

        //创建一个请求对象

        SKProductsRequest*request=[[SKProductsRequest alloc]initWithProductIdentifiers:set];

        //设置代理

        request.delegate=self;

        //启动请求

        [request start];

        //创建监听对象 来监听交易队列中交易对象的交易状态

        [[SKPaymentQueue defaultQueue]addTransactionObserver:self];

        

    }

     

    //代理方法,返回请求对象的数据列表

    -(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response

    {

        for (SKProduct*product in response.products) {

            //对象中包含商品对象的所有信息

        }

        self.allProducts=response.products;

        //刷新表格

        [self.tableView reloadData];

    }

    //2.展示可销售商品

    -(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:@"%@",product.price];

        

        return cell;

    }

     

    //3点击购买

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

        //4开票

        SKProduct *pro=self.allProducts[indexPath.row];

        SKPayment *payment=[SKPayment paymentWithProduct:pro];

        //5,创建交易对象,添加到交易队列

        //NSString *str=[NSBundle mainBundle]pathForResource:@"blue.bundle/blue/" ofType:nil;

        [[SKPaymentQueue defaultQueue]addPayment:payment];

    }

     

     

    //监听交易队列 的交易状态 改变的时候就会调用

    -(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

    {

        //非消耗品  才能被恢复购买

        /*

         SKPaymentTransactionStatePurchasing,    交易正在被添加到交易队列

         SKPaymentTransactionStatePurchased,     //交易已经在队列,用户已经付钱,客户端需要完成交易

         SKPaymentTransactionStateFailed,        //还没添加到队列中就取消或者失败了

         SKPaymentTransactionStateRestored,      // 交易被恢复购买,客户端需要完成交易

         SKPaymentTransactionStateDeferred NS_ENUM_AVAILABLE_IOS(8_0),   交易在队列中,交易状态不确定依赖别的参数参与

         */

        //7.如果交易成功提供 交易的产品

        for(SKPaymentTransaction *t in transactions)

        {

            if (t.transactionState==SKPaymentTransactionStatePurchased) {

                //提供服务

                

                //结束交易

                [[SKPaymentQueue defaultQueue]finishTransaction:t];

            }else if(t.transactionState==SKPaymentTransactionStateRestored)

            {

                //恢复购买成功

                

                //结束交易

                [[SKPaymentQueue defaultQueue]finishTransaction:t];

            }

        }

        

        

    }

     

    -(void)dealloc

    {

        [[SKPaymentQueue defaultQueue]removeTransactionObserver:self];

     

    }

     

     

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        }

     

    @end

  • 相关阅读:
    总结jQuery选择器
    Responsive布局技巧
    学习资料
    大公司开源项目【转载】
    针对css3特性浏览器兼容 封装less
    手把手教你nginx/linux下如何增加网站
    又一枚神器:nginx
    http://www.howtocn.org/nginx
    nginx修改内核参数
    Nginx
  • 原文地址:https://www.cnblogs.com/tangranyang/p/4655875.html
Copyright © 2020-2023  润新知