一.设置付费协议
1.进入开发者平台,点击如下图所示:
image.png设置
2.查看付费同意条款
image.png- 设置付费协议
添加这3个信息
4.添加银行账户
image.png5.选择报税表
image.png默认美国
6.根据报税表填写报税情况,根据自己实际情况,在这里我们需要选择NO,后面选了几个勾,以为可以,后面报错了!
image.png三者必须填写 注意:这里可以随便填写。
7.填写联系人,这几个部分可以填写同一人
image.png二.添加内购项目
- 点击添加
2.选择内购项目类型
image.png根据功能选择
3.设置产品价格及名称
image.png4.显示信息
image.png5.审核信息
image.png注意:这些信息都要填完整,否则会提示元数据丢失。
image.png内购项目
三.创建沙盒技术测试号
image.png添加账号
image.png三.代码集成
在需要购买的控制器内
#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:{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"buyed"];
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
}
break;
case SKPaymentTransactionStatePurchasing:
break;
case SKPaymentTransactionStateRestored:{
[[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.本身请求美国服务器就慢,为防止审核人员误解,我们需要在购买时加载动画。
网友评论