2013-03-27 2 views
0

앱에서 앱내 구매를 구현했으며 그것에 대해 두 가지 질문이 있습니다.iOS 인앱 구매에서 이전의 지갑을 안전하게 복원합니다.

- (IBAction)buyProduct1:(id)sender { 
    SKPayment *payment = [SKPayment paymentWithProduct:product1]; 
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 
    [[SKPaymentQueue defaultQueue] addPayment:payment]; 
} 

- (IBAction)restorePurchases:(id)sender { 
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions]; 
} 

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { 

    for (SKPaymentTransaction *transaction in transactions) { 
     switch (transaction.transactionState) { 
      case SKPaymentTransactionStatePurchasing: 

       break; 
      case SKPaymentTransactionStatePurchased: 

       //Can I be 100% sure that this method is only called when the product is bought? 
       [self product1Bought:transaction]; 

       break; 
      case SKPaymentTransactionStateRestored: 

       //What code should I have here to safely restore product1 if the product is bought? 

       break; 
      case SKPaymentTransactionStateFailed: 

       break; 
      default: 
       break; 
     } 
    } 
} 

- (void)product1Bought:(SKPaymentTransaction *)transaction { 
    NSString *string = @"Product1"; 
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"Product1"]; 
    NSLog(@"Product1 is bought"); 
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; 
} 

1.I 그냥 궁금 나는 성공적으로 구입 후 이상한 상황이 발생할하지 않을 경우 제품이 아니었다 productBought 방법 만라는 것을 100 % 확신 할 수있는 경우 :이 코드는 내가 사용하는 것입니다 정말 사 왔어?

2. product1Bought 메소드를 호출 할 수 있도록 product1이 먼저 구매되었는지 어떻게 확인할 수 있습니까?

답변

2
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue 

{

if ([queue.transactions count] == 0) 
{ 
    UIAlertView *restorealert = [[UIAlertView alloc] 
            initWithTitle:@"Restore" 
            message:@"There is no products purchased by you" 
            delegate:self 
            cancelButtonTitle:@"Ok" 
            otherButtonTitles:nil]; 

    [restorealert show]; 
    activityIndicator.hidden = YES; 


} 
else 
{ 
    NSString *productID; 



    for(SKPaymentTransaction *transaction in queue.transactions) 
    { 
    productID = transaction.payment.productIdentifier; 
     NSLog(@"the product identifier is %@",productID); 

복원을 완료하면 여기에서 구입 한 제품 ID를 얻을 수 있습니다.

1

1) 잘 작동하고 초기 구입 한 항목에 대한 사용자의 구매 그것을

2) 당신이 SKPaymentTransactionStateRestored에서이 메서드를 호출 할 때 데이터를 사용할 수 있습니다 :

[self RestorePurchases]; 

-(void)RestorePurchases{ 
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]; 
}