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
posted @   #零下一度&  阅读(379)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
点击右上角即可分享
微信分享提示