一.设置付费协议
1.进入开发者平台,点击如下图所示:
2.查看付费同意条款
3. 设置付费协议
4.添加银行账户
5.选择报税表
6.根据报税表填写报税情况,根据自己实际情况,在这里我们需要选择NO,后面选了几个勾,以为可以,后面报错了!
注意:这里可以随便填写。
7.填写联系人,这几个部分可以填写同一人
二.添加内购项目
- 点击添加
2.选择内购项目类型
3.设置产品价格及名称
4.显示信息
5.审核信息
注意:这些信息都要填完整,否则会提示元数据丢失。
####三.创建沙盒技术测试号
####三.代码集成
在需要购买的控制器内
#import <StoreKit/StoreKit.h>
@interface HomeViewController ()<SKProductsRequestDelegate,SKPaymentTransactionObserver>
@end
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"总共免费使用5次,已消耗完" message:@"付费后可无限次使用" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"付费" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[IHUtility addWaitingView:@"支付中"];
//监听购买结果
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
if([SKPaymentQueue canMakePayments]){
[self requestProductData:@"com.IntelligentNetwork.QuickIdentificationDiagram.purchase1"];
}else{
NSLog(@"不允许程序内付费");
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
// 弹出对话框
[self presentViewController:alert animated:true completion:nil];
}
//去苹果服务器请求商品
- (void)requestProductData:(NSString *)type{
NSArray *product = [[NSArray alloc] initWithObjects:type,nil];
NSSet *nsset = [NSSet setWithArray:product];
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset];
request.delegate = self;
[request start];
}
//收到产品返回信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
NSLog(@"--------------收到产品反馈消息---------------------");
NSArray *product = response.products;
NSLog(@"productID:%@", response.invalidProductIdentifiers);
if(product.count==0){
[WHToast showMessage:@"查找不到商品信息" duration:1 finishHandler:^{
}];
return;
}
[IHUtility addWaitingView:@"支付中"];
SKProduct *p = nil;
for(SKProduct *pro in product) {
NSLog(@"%@", [pro description]);
NSLog(@"%@", [pro localizedTitle]);
NSLog(@"%@", [pro localizedDescription]);
NSLog(@"%@", [pro price]);
NSLog(@"%@", [pro productIdentifier]);
if([pro.productIdentifier isEqualToString: @"com.IntelligentNetwork.QuickIdentificationDiagram.purchase1"]){
p = pro;
}
}
SKPayment *payment = [SKPayment paymentWithProduct:p];
NSLog(@"发送购买请求");
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
//请求失败
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
[IHUtility removeWaitingView];
[WHToast showMessage:@"支付失败" duration:1 finishHandler:^{
}];
}
- (void)requestDidFinish:(SKRequest *)request{
[IHUtility removeWaitingView];
}
//监听购买结果
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transaction{
[IHUtility removeWaitingView];
for(SKPaymentTransaction *tran in transaction){
[self verifyPurchaseWithPaymentTransaction:[NSNumber numberWithInt:tran.transactionState]];
switch(tran.transactionState) {
case SKPaymentTransactionStatePurchased:{
NSLog(@"交易完成");
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
}
break;
case SKPaymentTransactionStatePurchasing:
NSLog(@"商品添加进列表");
break;
case SKPaymentTransactionStateRestored:{
NSLog(@"已经购买过商品");
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
}
break;
case SKPaymentTransactionStateFailed:{
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
[WHToast showMessage:@"购买失败" duration:1 finishHandler:^{
}];
}
break;
default:
break;
}
}
}
//交易结束
- (void)completeTransaction:(SKPaymentTransaction *)transaction{
NSLog(@"交易结束");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)dealloc{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
-(void)verifyPurchaseWithPaymentTransaction:(NSNumber*)resultState{
//从沙盒中获取交易凭证并且拼接成请求体数据
NSURL *receiptUrl=[[NSBundle mainBundle] appStoreReceiptURL];
NSData *receiptData=[NSData dataWithContentsOfURL:receiptUrl];
}
####四.注意事项
1.内购必须用真机测试
2.测试时必须退出App Store自己的Apple ID,登录沙盒的测试Apple ID .
3.本身请求美国服务器就慢,为防止审核人员误解,我们需要在购买时加载动画。