zoukankan      html  css  js  c++  java
  • iOS--内购的使用方法

    1.需要在https://developer.apple.com中进行设置
    2.需要导入这个框架

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

    @interface ViewController ()<SKProductsRequestDelegate,SKPaymentTransactionObserver>{
        SKProduct *product;
    }

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
      
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
       
        SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"com.lamco.1010"]];//创建请求,设置要请求的产品ID
        request.delegate = self;
        [request start];//开始请求
    }

    //请求成功代理方法
    -(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
        NSLog(@"产品列表:%@",response.products);
       
        product = [response.products firstObject];
       
        NSLog(@"名:%@,描述:%@,价格:%@",product.localizedTitle,product.localizedDescription,product.price);
       
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(40, 90, 300, 40);
        [btn setTitle:[NSString stringWithFormat:@"名:%@,描述:%@,价格:%@",product.localizedTitle,product.localizedDescription,product.price] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(gotoBuy) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
       
        NSLog(@"无效的产品列表:%@",response.invalidProductIdentifiers);
    }

    //请求失败
    -(void)request:(SKRequest *)request didFailWithError:(NSError *)error{
       
        NSLog(@"----%@",error);
    }

    -(void)gotoBuy{
        SKPayment *payment = [SKPayment paymentWithProduct:product];
       
        //是否允许支付
        if ([SKPaymentQueue canMakePayments]) {
            [[SKPaymentQueue defaultQueue] addPayment:payment];
        }
    }

    -(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
        NSLog(@"-----1:%@",transactions);
       
        SKPaymentTransaction *sk = [transactions firstObject];
        NSLog(@"=====%d,%@",sk.transactionState,sk.error);
    }

    -(void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions{
        NSLog(@"-----2");
    }

    -(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error{
        NSLog(@"------3");
    }

    -(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue{
        NSLog(@"------4");
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
    感谢您的访问! 若对您有帮助或有兴趣请关注博客:http://www.cnblogs.com/Rong-Shengcom/
  • 相关阅读:
    SQL Server 自动备份数据脚本
    数据库还原,System.Data.SqlClient.SqlError: 因为数据库正在使用,所以无法获得对数据库的独占访问权。 (Microsoft.SqlServer.SmoExtended)
    AD 域中给AD 用加登录本地计算的权限
    share point CSOM 客户端模式 创建表 增删改查
    .net 修改AD域中的密码
    ES6——Object.assign() 对象的合并
    nodeJs —— 从零搭建一个koa项目
    nodeJs —— mongoose学习及案例
    nodeJs —— koa 常用中间件
    js计算舍入误差解决办法
  • 原文地址:https://www.cnblogs.com/Rong-Shengcom/p/5190050.html
Copyright © 2011-2022 走看看