应用程序内购买从队列中删除

时间:2013-05-16 11:49:10

标签: ios objective-c in-app-purchase

我是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 个答案:

答案 0 :(得分:1)

我解决了我的问题:)我刚添加了这一行[[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;

            }

        }
    }

}