我正在学习如何使用应用内购买,并且最近在我的第一个应用中实现了它。为了在我的手机上测试它,我创建了两个测试帐户并为每个帐户购买了一个项目,两者都不同。当我删除应用程序并重新打开它时,它会尝试恢复两个帐户的数据并提供升级,无论我是否登录。 (正在调用paymentQueue: updatedTransactions:
并在状态SKPaymentTransactionStateRestored
)
这可以通过验证收据来解决吗?我已经对它进行了调查,但由于看起来有点令人生畏,所以一直把它推迟。希望在测试多个帐户时这只是一个问题。
作为参考,我遵循了本指南,但在自定义类中的App Delegate和Product Request中执行了Transaction Observer方法。我也从未对交易进行重新调整,因为显然这已经过时了。 http://troybrant.net/blog/2010/01/in-app-purchases-a-full-walkthrough/
编辑:我刚刚进行了一些测试,发现在作为观察者添加后,在启动时队列中有0个事务,但在调用uidatedTransactions之后有3个。
我的参考代码(对不起,如果它不好我还是新的):
#pragma mark - SKPaymentTransactionObserver
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
NSLog(@"Transactions Count: %d", [[[SKPaymentQueue defaultQueue] transactions] count]);
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
[self comleteTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
default:
break;
}
}
}
//case methods
- (void)comleteTransaction:(SKPaymentTransaction *)transaction
{
[self provideContent:transaction.payment.productIdentifier];
[self finishTransaction:transaction wasSuccessful:YES];
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction
{
[self provideContent:transaction.originalTransaction.payment.productIdentifier];
[self finishTransaction:transaction.originalTransaction wasSuccessful:YES];
}
- (void)failedTransaction:(SKPaymentTransaction *)transaction
{
if (transaction.error.code != SKErrorPaymentCancelled)
[self finishTransaction:transaction wasSuccessful:NO]; //serious error
else
[[SKPaymentQueue defaultQueue] finishTransaction:transaction]; //user cancelled
}
//action methods
- (void)provideContent:(NSString *)productID
{
//validate app receipt?
if ([productID isEqualToString:UNLOCK_EVERYTHING_ID])
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:UNLOCK_EVERYTHING_PURCHASED_KEY];
else if ([productID isEqualToString:REMOVE_ADS_ID])
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:REMOVE_ADS_PURCHASED_KEY];
else if ([productID isEqualToString:BASIC_PACK_ID])
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:BASIC_PACK_PURCHASED_KEY];
else if ([productID isEqualToString:CLOTHING_PACK_ID])
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:CLOTHING_PACK_PURCHASED_KEY];
else if ([productID isEqualToString:FOOD_PACK_ID])
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:FOOD_PACK_PURCHASED_KEY];
else if ([productID isEqualToString:ORIGINAL_PACK_ID])
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:ORIGINAL_PACK_PURCHASED_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful
{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
//post notification that transaction finished
if (wasSuccessful) {
[[NSNotificationCenter defaultCenter] postNotificationName:TRANSACTION_SUCCESSFUL object:nil];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:TRANSACTION_UNSUCCESSFUL object:nil];
}
}
答案 0 :(得分:1)
我没有查看该教程,但如果购买过程的任何部分向钥匙串添加了详细信息,则删除应用程序并重新安装它将使钥匙串条目保持不变。该应用程序可能正在检查购买状态...请参阅此处
答案 1 :(得分:0)
如果其他人遇到同样的问题,我只是通过更改RestoreState案例来解决它,以完成与PurchasedState案例相同的事情。事实证明,使用原始事务进行恢复是不必要的,只会导致问题。