继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

iOS苹果内购(详细步骤)

honey缘木鱼
关注TA
已关注
手记 12
粉丝 2
获赞 23

一.设置付费协议

1.进入开发者平台,点击如下图所示:
设置
2.查看付费同意条款

3. 设置付费协议
添加这3个信息
4.添加银行账户

5.选择报税表
默认美国
6.根据报税表填写报税情况,根据自己实际情况,在这里我们需要选择NO,后面选了几个勾,以为可以,后面报错了!
三者必须填写
注意:这里可以随便填写。

7.填写联系人,这几个部分可以填写同一人

二.添加内购项目

  1. 点击添加

    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.本身请求美国服务器就慢,为防止审核人员误解,我们需要在购买时加载动画。

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP