iOS的应用内购买 iAP 坑 之二

iOS Jan 19, 2015

闪退问题已找到,线程的问题,下面的代码刚好避开了引起错误的那段,没问题了。

补充于19号上午00:17

之一 中,写的有点乱,主要是之前的准备太繁琐,这次主要说代码。这次用StoreKit。

IAPHelper 用法自行 Github ,这次我主要讲 StoreKit的用法。

先打打预防针,我说下都有哪些坑

  • 第三方库,无法准确的获取 已经购买的项目 。也可能是我用的姿势不对。
  • 系统自带的,老特么闪退,概率在 1/5 ,点进去内购页,退出,点进去,退出,点进去,我擦,闪退了。

第三方库不说了, IAPHelper 用法自行 GithubCargoBay,文档写的那是个屁啊,鬼能看懂,RMStore 更蛋疼,导入项目,编译就报错,缺什么文件。

总之,我水平低,玩不了这么高级玩意儿。无奈来研究系统自带的了。

博主比较懒啦,能省的就省,至于怎么抽方法,你们自己看着来啦。

1.控制器中添加两个属性,一个代理协议一个观察者协议

@interface DonateTableViewController () <SKProductsRequestDelegate, SKPaymentTransactionObserver>
{
    NSArray *_productArr;
    NSMutableArray *_purchasedArr;
}

2.添加观察者对象为自身

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

3.创建一个请求

if ([SKPaymentQueue canMakePayments]) {
        // 没有意外
        NSSet *set =[NSSet setWithArray:@[@"eduadm_donate_3", @"eduadm_donate_6", @"eduadm_donate_12", @"eduadm_donate_25"]];
        
        SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
        
        request.delegate = self;
        [request start];
        
    }else {
    
        UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"访问限制" message:@"您已经禁止应用内购买" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        
        [view show];
    }

4.解析可购买产品数据

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    _productArr = response.products;
    
    [MBProgressHUD hideHUD];
    [self.tableView reloadData];
}

5.把产品添加到队列

SKPayment *payment = [SKPayment paymentWithProduct:product];
    
// 添加到队列
[[SKPaymentQueue defaultQueue] addPayment:payment];

6.监听

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *trans in transactions) {
        
        switch (trans.transactionState) {
            case SKPaymentTransactionStatePurchased:
                
                [self completeTransaction:trans];
                break;
                
            case SKPaymentTransactionStateFailed:
                
                [self failedTransaction:trans];
                break;
                
            case SKPaymentTransactionStateRestored:
                
                [self restoreTransaction:trans];
                break;
                
                
            default:
                break;
        }
        
    }

}

7.监听的补充

- (void)completeTransaction:(SKPaymentTransaction *)transaction {

    // 处理 id
    [self provideContentForProductIdentifier:transaction.payment.productIdentifier];
    
    // 移除队列
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}


- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
    
    [self provideContentForProductIdentifier:transaction.originalTransaction.payment.productIdentifier];
    
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

- (void)failedTransaction:(SKPaymentTransaction *)transaction {
    
    if (transaction.error.code != SKErrorPaymentCancelled) {
        
    }
    
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}


- (void)provideContentForProductIdentifier:(NSString *)productIdentifier {

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [_purchasedArr addObject:productIdentifier];
    [self.tableView reloadData];
}

The End

运行是没问题的,就是偶尔崩了,别找我,我不会

Tags

Jie Li

🚘 On-road / 📉 US Stock / 💻 Full Stack Developer / 🎓 Grad Student / ®️ ENTJ

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.