草庐IT

iOS In App purchase updatedTransactions : cannot distinguish between new purchase and restore

coder 2024-01-12 原文

据我所知,Apple 建议获得购买和恢复按钮(我在我的应用程序的设置 View 中有这些),在其他 View 中我只有购买按钮。

当用户点击“购买”按钮,苹果检测到这个用户已经购买了这个产品,他会要求用户免费恢复这次购买(这里一切正常)。当用户单击"is"时,将调用 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;
        }
    }
}

最佳答案

购买您已经拥有的东西(尽管 UIAlert 说它正在恢复)会触发 SKPaymentTransactionStatePurchased 状态。 SKPaymentTransactionStateRestored 状态仅在您执行以下操作时发生:

 [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];

关于iOS In App purchase updatedTransactions : cannot distinguish between new purchase and restore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23981485/

有关iOS In App purchase updatedTransactions : cannot distinguish between new purchase and restore的更多相关文章

随机推荐