我知道Apple建议购买和恢复按钮(我在应用程序的设置视图中有这些按钮),在其他视图中我只有购买按钮。
当用户点击“购买”按钮并且Apple检测到此用户已购买此产品时,他会要求用户免费恢复此购买(此处一切正常)。当用户点击“是”时,系统会调用updateTransactions:
,并且它始终位于case SKPaymentTransactionStatePurchased:
而不是case SKPaymentTransactionStateRestored:
。
为什么?有没有办法区分还原和新购买与updatedTransactions:
?
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
for(SKPaymentTransaction *transaction in transactions){
switch (transaction.transactionState){
case SKPaymentTransactionStatePurchasing: //NSLog(@"Transaction state -> Purchasing");
//called when the user is in the process of purchasing, do not add any of your own code here.
break;
case SKPaymentTransactionStatePurchased:
//this is called when the user has successfully purchased the package (Cha-Ching!)
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"Purchase" action:@"Purchase Completed!" label:shopNameSelected value:nil] build]];
[self doGoPremium];
[MBProgressHUD hideHUDForView:self.view animated:YES];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
//NSLog(@"Transaction state -> Purchased");
break;
case SKPaymentTransactionStateRestored:
//NSLog(@"Transaction state -> Restored Here");
//add the same code as you did from SKPaymentTransactionStatePurchased here
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"Purchase" action:@"Purchase Restored" label:shopNameSelected value:nil] build]];
[self doGoPremium];
[MBProgressHUD hideHUDForView:self.view animated:YES];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
//called when the transaction does not finnish
[MBProgressHUD hideHUDForView:self.view animated:YES];
if(transaction.error.code != SKErrorPaymentCancelled){
//NSLog(@"Transaction state -> Cancelled");
//the user cancelled the payment ;(
// Add some analytics point.
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"Purchase" action:@"Purchase Canceled" label:shopNameSelected value:nil] build]];
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
答案 0 :(得分:1)
购买您已经拥有的东西(尽管UIAlert说它正在恢复)会触发SKPaymentTransactionStatePurchased状态。 SKPaymentTransactionStateRestored状态仅在您执行以下操作时发生:
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];