我正在进行应用内购买,但我有一个错误,我找不到它的来源......
首先,我有一个按钮,当你点击它时,我的应用程序请求产品,捕捉响应,你支付,你得到产品(一切正常)。但是我的问题来了。如果我点击按钮再次购买任何东西,我会收到两次提醒我买了两次东西。即使我第三次点击,我也会收到三个警报,说我买了东西 3 次,根据我点击的次数,有 4 次和 5 次。
所以似乎一些变量一直在存储请求。这是我的代码:
这将验证产品 ID
- (void) validateProductIdentifiers
{
NSString *monedas = @" ID FROM PRODUCT ";
NSSet *product = [NSSet setWithObject:monedas];
productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:product];
productsRequest.delegate = self;
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[productsRequest start];
NSLog(@"STARTED REQUEST");
}
这处理来自Apple的响应
- (void)productsRequest:(SKProductsRequest *)request
didReceiveResponse:(SKProductsResponse *)response
{
productsRequest=nil;
int found=0;
SKProduct *paraPagar;
skProducts = response.products;
for (SKProduct * skProduct in skProducts) {
NSLog(@"Found product: %@ %@ %0.2f",
skProduct.productIdentifier,
skProduct.localizedTitle,
skProduct.price.floatValue);
found=1;
paraPagar = skProduct;
}
if (found==1){
payment = [SKMutablePayment paymentWithProduct:paraPagar];
payment.quantity = 1;
[[SKPaymentQueue defaultQueue]addPayment:payment];
}else{
//error (not relevant code)
}
}
接受付款并完成交易
- (void)paymentQueue:(SKPaymentQueue *)queue
updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
NSLog(@"BOUGHT");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
NSLog(@"FAILED");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[self failedTransaction];
break;
case SKPaymentTransactionStateRestored:
NSLog(@"RESTORED");
//[self restoreTransaction:transaction];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
case SKPaymentTransactionStatePurchasing:
NSLog(@"PURCHASING");
default:
break;
}
}
}
非常感谢您的宝贵时间!