除了添加之外restoreCompletedTransactions
,您还需要处理您的 iaps 是如何实际恢复给用户的,以及内容是如何提供给用户的。
基本上,您需要回忆您在用户最初购买商品时向他们提供商品的方式。
例如,这就是我在我的一个应用程序中恢复产品的方式。
恢复事务
- (void)restoreTransaction:(SKPaymentTransaction *)transaction
{
isRestoring = YES;
[self recordTransaction: transaction];
/* This is where I provide the content to the user: */
[self provideContent: transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
交易完成
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
[self recordTransaction: transaction];
[self provideContent: transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
支付队列
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code == SKErrorPaymentCancelled) {
/// user has cancelled
[[NSNotificationCenter defaultCenter] postNotificationName:@"hideRestoring" object:nil];
}
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
}
}
提供内容
- (void)provideContent:(NSString *)productIdentifier
{
int index;
NSMutableDictionary * purchased = [NSMutableDictionary dictionaryWithContentsOfFile:EXTRAS_PATH];
NSArray * availableProducts = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:SCANNER_IN_APP_PURCHASES]];
if ( isRestoring )
{
for ( index = 0; index < [availableProducts count]; index++ )
{
//NSLog(@"productIdentifier: %@",productIdentifier);
//NSLog(@"Product: %@",[availableProducts objectAtIndex:index]);
if ( [productIdentifier isEqualToString:[[availableProducts objectAtIndex:index] objectForKey:@"BundleID"]] )
{
[purchased setObject:[availableProducts objectAtIndex:index] forKey:productIdentifier];
[purchased writeToFile:EXTRAS_PATH atomically:YES];
[_purchasedProducts addObject:productIdentifier];
}
}
}
else
{
index = [[[NSUserDefaults standardUserDefaults] objectForKey:@"kTempProductPurchasingIndex"] intValue];
if ( [productIdentifier isEqualToString:[[availableProducts objectAtIndex:index] objectForKey:@"BundleID"]] )
{
[purchased setObject:[availableProducts objectAtIndex:index] forKey:productIdentifier];
[purchased writeToFile:EXTRAS_PATH atomically:YES];
[_purchasedProducts addObject:productIdentifier];
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:kProductPurchasedNotification object:productIdentifier];
}