2013-05-16 48 views
0

我是iOS新手,我嘗試從我的服務下載3個產品,但用戶需要購買這些產品,我有3個按鈕,我點擊第一個,然後輸入用戶名和密碼,它是下載,然後我點擊第二個產品,首先是下載第一個產品,然後是第二個產品,然後我點擊第三個產品,它是下載第一,第二,第三個產品。我可以從隊列中刪除第一個產品下載第一個產品後?對不起,我的英語不好。應用內購買從隊列中刪除

謝謝。

最好的問候。

- (IBAction)downloadButtonPressed:(id)sender { 
     SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.company.testtest123"]; 
     [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; 
     [[SKPaymentQueue defaultQueue]addPayment:payment]; 
    } 


      - (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions { 
    for (SKPaymentTransaction *transaction in transactions) { 
     // Count down 
     @synchronized(self) { 
      NSInteger numOfTransaction = [[NSUserDefaults standardUserDefaults] integerForKey:@"NumberOfTransactions"]; 
      [[NSUserDefaults standardUserDefaults] setInteger:numOfTransaction-1 forKey:@"NumberOfTransactions"]; 
      [[NSUserDefaults standardUserDefaults] synchronize]; 
     } 
    } 
} 
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{ 
    SKProduct *validProduct =nil; 

    int count = [response.products count]; 

    if (count>0) { 
     validProduct =[response.products objectAtIndex:0]; 
    }else if(!validProduct){ 

     NSLog(@"No products avaible"); 

    } 

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

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

       break; 

      case SKPaymentTransactionStatePurchased:{ 
       //download kismi buraya 

       NSLog(@"_mainDict = %@",_mainDict); 

       float _csize = (float) 0/1024; 
       float _tsize = (float) [[_mainDict valueForKey:@"Size"] integerValue]/1024; 
       NSString *strDownload = [NSString stringWithFormat:@"downloading \n (%0.2f MB/%.2f MB)",_csize,_tsize]; 
       [SVProgressHUD showProgress:0 status:strDownload maskType:SVProgressHUDMaskTypeBlack]; 


       [service servicePicList]; 


       [[SKPaymentQueue defaultQueue]finishTransaction:transaction]; 

       break; 
      } 

      case SKPaymentTransactionStateRestored:{ 
       [[SKPaymentQueue defaultQueue]finishTransaction:transaction]; 


       break; 
      } 

      case SKPaymentTransactionStateFailed: 
      { 
       if (transaction.error.code != SKErrorPaymentCancelled) { 
        NSLog(@"An error encountered"); 
       } 

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

      } 

     } 
    } 

} 




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

    NSLog(@"failedTransaction..."); 
    if (transaction.error.code != SKErrorPaymentCancelled) 
    { 
     NSLog(@"Transaction error: %@", transaction.error.localizedDescription); 
    } 

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction]; 
} 

回答

1

我解決了我的問題:)我只是說此行[SKPaymentQueue defaultQueue] removeTransactionObserver:自我]。現在它正在工作...... :)

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

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

       break; 

      case SKPaymentTransactionStatePurchased:{ 

       //I added this line. 
       [[SKPaymentQueue defaultQueue] removeTransactionObserver:self]; 


       NSLog(@"_mainDict = %@",_mainDict); 

       float _csize = (float) 0/1024; 
       float _tsize = (float) [[_mainDict valueForKey:@"Size"] integerValue]/1024; 
       NSString *strDownload = [NSString stringWithFormat:@"downloading \n (%0.2f MB/%.2f MB)",_csize,_tsize]; 
       [SVProgressHUD showProgress:0 status:strDownload maskType:SVProgressHUDMaskTypeBlack]; 


       [service servicePicList]; 


       [[SKPaymentQueue defaultQueue]finishTransaction:transaction]; 

       break; 
      } 

      case SKPaymentTransactionStateRestored:{ 
       [[SKPaymentQueue defaultQueue]finishTransaction:transaction]; 


       break; 
      } 

      case SKPaymentTransactionStateFailed: 
      { 
       if (transaction.error.code != SKErrorPaymentCancelled) { 
        NSLog(@"An error encountered"); 
       } 

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

      } 

     } 
    } 

} 
+2

不,那不行。 '[[SKPaymentQueue defaultQueue] removeTransactionObserver:self]'線讓你的對象不接收消息。但它對交易沒有任何作用。要從隊列中移除一個事務,你可以調用'finishTransaction'。 –