现在的位置: 首页 > 综合 > 正文

In-App Purch – Your first In-App Purchase(s) must be submitted with a new app version…问题解决方案

2016年01月21日 ⁄ 综合 ⁄ 共 4219字 ⁄ 字号 评论关闭

应用内购买
In-App Purchase
在很多APP中被嵌入,实现一个内购产品,不光要写大量代码,更要在苹果官方配置页面做大量的配置,下面我讲简单的说一下整个开发和配置流程:

1,开发一款APP,最好是支持内购形式的,在开发中需要引入StoreKit.framework这个库文件;

2,实现具体的相关代码,先这里写出来,后面再说配置:

 实现页面引入  //#import<StoreKit/StoreKit.h>。

 加入协议 <DMOfferWallManagerDelegate,SKProductsRequestDelegate,SKPaymentTransactionObserver>

- (void)viewDidLoad
{
 //比较重要,不要忘记了
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}

判断权限

if ([SKPaymentQueue canMakePayments]) {
                    [self requestProductData];
                    NSLog(@"允许程序内付费购买");
                }else {
                    NSLog(@"不允许程序内付费购买");
                    UIAlertView *alerView =  [[UIAlertView alloc] initWithTitle:nil
                                                                        message:@"不允许应用程序内购买,\n请重新设置该功能后再购买"
                                                                       delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil];
                    [alerView show];
                }


购买函数及其回调函数

-(void)requestProductData
{
    NSSet *productSet = [NSSet setWithArray:@[KIAP_NAME]];
    SKProductsRequest *request=[[SKProductsRequest alloc] initWithProductIdentifiers:productSet];
    request.delegate=self;
    [request start];
    
    self.topVC.title = @"正在购买...";
}

#pragma mark - SKProductsRequestDelegate
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    NSArray *product = response.products;
    if (product.count == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"无法获取产品信息,请稍后再试" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil];
        [alert show];
        NSLog(@"无法获取产品信息,购买失败。");
        
        self.topVC.title = self.titleStr;
        return;
    }
    SKPayment *payment = [SKPayment paymentWithProduct:product[0]];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
    // process....
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"购买失败,请稍后再试" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil];
    [alert show];
    self.topVC.title = self.titleStr;
}

#pragma mark - SKPaymentTransactionObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased://交易完成
                NSLog(@"transactionIdentifier = %@", transaction.transactionIdentifier);
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed://交易失败
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored://已经购买过该商品
                [self restoreTransaction:transaction];
                break;
            case SKPaymentTransactionStatePurchasing:      //商品添加进列表
                NSLog(@"商品添加进列表");
                break;
            default:
                break;
        }
    }
    
}

- (void)completeTransaction:(SKPaymentTransaction *)transaction {
    // Your application should implement these two methods.
    NSString * productIdentifier = transaction.payment.productIdentifier;
    
    NSLog(@"productIdentifier:%@",productIdentifier);

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"购买成功,你已经是土豪了" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil];
    [alert show];
    
    self.topVC.title = self.titleStr;
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kIAP_MONEY];
}

- (void)failedTransaction:(SKPaymentTransaction *)transaction {
    if(transaction.error.code != SKErrorPaymentCancelled) {
        NSLog(@"购买失败");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"购买失败,请稍后再试" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil];
        [alert show];
        
    } else {
        NSLog(@"用户取消交易");
    }
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
    
    self.topVC.title = self.titleStr;
}

- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
    // 对于已购商品,处理恢复购买的逻辑
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

当然了具体的安装自己的要求去写,这里有个

<span style="font-size:12px;">KIAP_NAME //是我们在配置页面申请的产品序列号,当然可以加入很多个了</span>

写到这里,代码部分就完成了,开始配置页面

3,打开苹果开发者页面,必须99美元喔,然后选中你配置的应用信息,当然了如果版本更新就直接创建新版本,如果是第一次,先提交一个APP,便于测试,我这里就说版本更新时候吧,首先创建一个新的版本APP信息,然后点击Manage
In-App Purchases进入配置页面,里面创建一个产品,名称可以是com.xxx.xxx.product1 ,这个名称就是前面说的KIAP_NAME了,当然了要提交一个关于购买的图片,这个可以是测试时候提示的那个沙盒图片;


4, 在测试之前你要到用户管理里面添加一个test用户,然后激活就行,当时真机测试时候就用这个账户购买即可,然后会返回各种提示信息等;

5,希望你开通直接的支付信息,这块也是个大问题,查查资料吧,应该能搞定;

6,当你设置完这些后就可以测试了,当在沙盒测试成功之后,我们理所应当的去提交苹果审核了,这里就非常值得主要一点,否则就如同我一般提交审核通过才发现我的内购产品这里还是黄色警告,如题目

7,在创建完一个新版本后状态应该是等待上传吧,这时候我们要看在view detail页面有个选项是关于内购的,点击edit后可以看到我们之前设置的产品,勾选即可,然后在点击右上角的准备上传代码


8 ,当状态改变为准备上传后再去看内购页面的黄色警告已经没有了,现在就可以安心的archive你的代码去审核了,第7步骤是关键,不要忽视了,否则如同我一般从新来提交一个修改版本啦

当依次完成上面的步骤后就实现了一个内购的产品,具体很多细节可以参考具体文章,写的比较详细,我这里只是说下步骤和比较容易忽视的地方,希望对大家有所帮助。

后语:

实例项目:IT面试宝典 

支持个人开发者,欢迎加入,让想象变成可能  微推 - www.micropush.cn


抱歉!评论已关闭.