我在我的应用程序中进行了应用程序内购买,它们只是一次性非消耗品购买。他们完全工作,甚至在应用程序商店中实现。 然而,在看似随机的时间,当用户试图从Apple购买响应时非常慢,例如在100Mbps网络或LTE / 3G上通过Wi-Fi的30秒以上。它似乎与连接没有任何关系,只需要很长时间才能得到Apple的响应。
以下是创建此提示并处理响应的基本代码:
// User has decided to buy a sticker pack
- (void) promptToBuyPack:(StickerPack *)pack {
if([SKPaymentQueue canMakePayments]) {
if([products valueForKey:pack.packID]) {
SKPayment *payment = [SKPayment paymentWithProduct:[products valueForKey:pack.packID]];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
else {
nlog(@"User attempted to buy a pack that iTunes does not recognize");
}
}
else {
UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Can not make purchases" message:@"Your device does not have purchases enabled. Please enable them in order to proceed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[a show];
}
}
- (void) grantProductAccess:(NSString *)productIdentifier {
// Store purchase in user defaults
nlog(@"Bought product: %@", productIdentifier);
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:YES] forKey:productIdentifier];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void) tellUserPurchaseIsComplete {
UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"Purchase Complete" message:@"Your pack is now unlocked, Enjoy!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[a show];
[a release];
}
- (void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for(SKPaymentTransaction *transaction in transactions) {
BOOL canFinishTransaction = NO;
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
canFinishTransaction = YES;
[self grantProductAccess:transaction.payment.productIdentifier];
[self tellUserPurchaseIsComplete];
if(iPad)
[FlurryAPI logEvent:[NSString stringWithFormat:@"Purchased %@ on iPad",transaction.payment.productIdentifier]];
else
[FlurryAPI logEvent:[NSString stringWithFormat:@"Purchased %@ on iPod",transaction.payment.productIdentifier]];
break;
case SKPaymentTransactionStateFailed:
nlog(@"Transactin failed");
canFinishTransaction = YES;
break;
case SKPaymentTransactionStateRestored:
nlog(@"SKPaymentTransactionStateRestored");
canFinishTransaction = YES;
[self grantProductAccess:transaction.originalTransaction.payment.productIdentifier];
break;
default:
nlog(@"SKPaymentTransactionDefault");
break;
}
// Remove from payment queue
if(canFinishTransaction)
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
}