2016-10-09 3 views
0

IAP(この例では2つ)にいくつかの製品があり、ユーザーが購入したい製品をクリックして選択するUITableViewを使用しています。しかし、私は、ユーザーが購入したトランザクションをupdatedTransactionsデリゲートで識別することができません。以下は、私のコードは次のとおりです。updatedTransactionsデリゲートで購入した製品を特定できません

NSArray *arrayProducts; 

-(void) getProductInfo:(BLevelViewController *) viewController 
{ 
if ([SKPaymentQueue canMakePayments]) 
{ 
    _linkStatus.text = @"Able to purchase";   
    SKProductsRequest *requestProducts = [[SKProductsRequest alloc] 
    initWithProductIdentifiers:[NSSet setWithObjects:@"example.1coin", @"example.2coin", nil]]; 
    requestProducts.delegate = self; 
    [requestProducts start]; 

}else{ 
     _linkStatus.text = @"Please enable In App Purchase in Setting"; 
}  
} 

-(void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response 
{ 
    NSArray *products = response.products; 

    if (products.count != 0) 
    { 
     _linkStatus.text = @"Product loaded"; 
     arrayProducts = products; 

    } else { 
     _linkStatus.text = @"Product no found"; 
     arrayProducts = @[@"0",@"0"]; 
    } 

    products = response.invalidProductIdentifiers; 

    for (SKProduct *product in products) 
    { 
     NSLog(@"Product not found: %@", product); 
    } 

} 


-(void) tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

if (indexPath.row == 0) 
{ 
    SKPayment *payment = [SKPayment paymentWithProduct:[arrayProducts objectAtIndex:0]]; 
    [[SKPaymentQueue defaultQueue] addPayment:payment]; 

} else if (indexPath.row == 1) 
{ 
    SKPayment *payment = [SKPayment paymentWithProduct:[arrayProducts objectAtIndex:1]]; 
    [[SKPaymentQueue defaultQueue] addPayment:payment]; 
} 

} 

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions 
{ 
    for (SKPaymentTransaction *transaction in transactions) 
    { 
     switch (transaction.transactionState) { 
      case SKPaymentTransactionStatePurchased: 
       if ([transaction.description isEqualToString:[arrayProducts objectAtIndex:0]]){ 
        [self unlockFeature1coin]; 
       } 
       if ([transaction.description isEqualToString:[arrayProducts objectAtIndex:1]]){ 
        [self unlockFeature10coins]; 
       } 
       [[SKPaymentQueue defaultQueue] 
       finishTransaction:transaction]; 
       break; 

      case SKPaymentTransactionStateFailed: 
       [[SKPaymentQueue defaultQueue] 
       finishTransaction:transaction]; 
       break; 

      case SKPaymentTransactionStatePurchasing: 

       break; 


      default: 
       break; 
     } 
    } 
} 

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    BCoinsViewCell * cell = [tableView dequeueReusableCellWithIdentifier:coinsCellIdentifier]; 

    if (indexPath.row == 0) 
    { 
     cell.cellLabel.text = @"Buy 10 coins"; 
     [cell.cellButton setTitle:@"Buy" forState:UIControlStateNormal]; 
    } 
    else if (indexPath.row == 1) 
    { 
     cell.cellLabel.text = @"Buy 1 coin"; 
     [cell.cellButton setTitle:@"Buy" forState:UIControlStateNormal]; 
    } 

    return cell; 
} 

問題は、私は、ユーザーの購入に基づいて異なる機能を呼び出すことができるように私は、識別子として何を使用する必要があり、以下の行にありますか?

 if ([transaction.description isEqualToString:[arrayProducts objectAtIndex:0]]){ 
      [self unlockFeature1coins]; 
     } 
     if ([transaction.description isEqualToString:[arrayProducts objectAtIndex:1]]){ 
      [self unlockFeature10coins]; 
     } 
+0

購入した製品の情報はどこに保管していますか?そして、 'cellForRowAtIndexPath:'メソッドの実装を表示できますか?私はあなたが不適切な方法でこの問題に近づいていると思います。 – Adeel

+0

cellForRowAtIndexPathでコードを更新しました。私が知っているように、AppleはgetProductInfoメソッドのための一般的なメソッドを提供していないので、私たちはハードコードをしなければなりません。したがって、cellForRowAtIndexPathで、私は2つの製品をハードコーディングしています。私はこれに正しくアプローチしていないかもしれないので、私はこれを処理すべきですか? – Calvin

+0

私はあなたに何かを提案する前に、さらに2つのことを知りたいと思います。あなたの購入は消耗品ではありませんか?その場合は、これがアプリ内購入の不可欠な部分であるため、購入を復元する方法を実装しています。実際には、Appleは購入を復元する方法を実装していない場合、あなたのアプリを拒否します。 – Adeel

答えて

1

あなたはこのようなtransaction.payment.productIdentifierSKPaymentTransactionオブジェクトから製品識別子を取得することができます。 if条件をこのコードで置き換えてください:

if ([transaction.payment.productIdentifier isEqualToString:@"example.1coin"]){ 

     [self unlockFeature1coins]; 
    } 
    if ([transaction.payment.productIdentifier isEqualToString:@"example.10coins"]){ 

     [self unlockFeature10coins]; 
    } 
+0

Adele Miraj、あなたは次のコードであなたの答えを更新できますか?アレイ製品が識別子として格納されていないため、アレイ製品を参照する際に問題があります。コードは以下のようになります。あなたの助けをもう一度ありがとう! if([transaction.payment.productIdentifier isEqualToString:@ "example.1coin"]){ [self unlockFeature1coins]; } if([transaction.payment.productIdentifier isEqualToString:@ "example.10coins"]){ [self unlockFeature10coins]; } – Calvin

+0

@カルヴンコードを更新しました。 – Adeel

関連する問題