我是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];
}

最佳答案

我解决了我的问题:)我刚刚添加了这一行[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];现在它正在工作... :)

    -(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;

            }

        }
    }

}

09-04 10:26