我希望在下载交易完成后完成付款交易,但是由于某种原因下载完成后交易状态未完成。下载完成后,我是否必须将支付交易放回队列中,这里有更多代码。
- (void)buyProduct:(SKProduct *)product {
NSLog(@"Buying %@...", product.productIdentifier);
SKPayment * payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for (SKPaymentTransaction * transaction in transactions) {
if(transaction.downloads){
[[SKPaymentQueue defaultQueue] startDownloads:transaction.downloads];
}else{
switch (transaction.transactionState){
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
}
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedDownloads:(NSArray *)downloads;
{
for (SKDownload *download in downloads) {
if (download.downloadState == SKDownloadStateFinished) {
[self processDownload:download]; //handle download
// now we're done
NSLog(@"Download finished");
[[SKPaymentQueue defaultQueue] finishTransaction:download.transaction];
//Do I need to add the payment transaction back in the queue? Im worried the payment will go through twice?
} else if (download.downloadState == SKDownloadStateActive) {
NSString *productID = download.contentIdentifier; // in app purchase identifier
NSTimeInterval remaining = download.timeRemaining; // secs
float progress = download.progress; // 0.0 -> 1.0
NSLog(@"Product ID:%@", productID);
NSLog(@"Time remaining: %f", remaining);
NSLog(@"Download progress percent: %f", progress);
} else { // waiting, paused, failed, cancelled
NSLog(@"Warn: not handled: %d", download.downloadState);
}
}
}
这些是我为交易状态创建的方法,如果它很重要的话?他们都没有被执行,所以我认为这不重要,但在这里。
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"completeTransaction...");
[self provideContentForProductIdentifier:transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"restoreTransaction...");
[self provideContentForProductIdentifier:transaction.originalTransaction.payment.productIdentifier];
if(transaction.downloads){
[[SKPaymentQueue defaultQueue] startDownloads:transaction.downloads];
NSLog(@"Download exists");
}else{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
}
- (void)failedTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"failedTransaction...");
if (transaction.error.code != SKErrorPaymentCancelled)
{
NSLog(@"Transaction error: %@", transaction.error.localizedDescription);
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}