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/