iOS应用内购买非消耗品确定是新购买还是恢复

发布于 2024-11-09 09:54:04 字数 395 浏览 0 评论 0原文

有没有办法查明用户何时购买非消耗品,是第一次购买还是已经购买并免费再次获得?

我检查了交易的 transactionStatetransactionDate,但在这两种情况下,数据是相同的:

  • transactionState: < code>SKPaymentTransactionStatePurchased(如果用户已经购买,则不是 SKPaymentTransactionStateRestored
  • transactionDate:添加交易的日期到AppStore的支付队列。

Is there a way to find out when a user buys a non-consumable, if he buys it for the first time or has bought it already and gets it again for free?

I checked transactionState, transactionDate of the transaction, but in both cases the data is the same:

  • transactionState: SKPaymentTransactionStatePurchased (and not SKPaymentTransactionStateRestored in case the user bought it already)
  • transactionDate: the date at which the transaction was added to the AppStore's payment queue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

徒留西风 2024-11-16 09:54:04

您可以检查在restoreTransaction方法之后将填充的交易数组,如果交易数组为空,则意味着用户第一次下载此升级。在另一种情况下,您将检查数组中的所有交易,并将 transaction. payment.productIdentifier 与所需的产品标识进行比较。如果不存在,则在交易数组中添加付款。

对于非消耗性应用内购买,我使用了以下代码:

#define kInAppPurchaseProUpgradeProductId @"upgradeProductId"

//...
//your payment code for all SKPaymentTransactionStates
//...

//method called when BUY button press
-(void)purchaseProUpgrade{
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
//when restore completed delegate method calls 
-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue{

    if([[[SKPaymentQueue defaultQueue] transactions] count]==0)            
        [self addNewPaymentForProductId:kInAppPurchaseProUpgradeProductId];
    else            
        for (SKPaymentTransaction *transaction in [[SKPaymentQueue defaultQueue] transactions]){

            if (![transaction.payment.productIdentifier isEqualToString:kInAppPurchaseProUpgradeProductId]){
                [self addNewPaymentForProductId:kInAppPurchaseProUpgradeProductId];
                break;
            }
        }    
}

-(void)addNewPaymentForProductId:(NSString *)productId{
    if([SKPaymentQueue canMakePayments]){
        SKPayment *payment = [SKPayment paymentWithProductIdentifier:productId];
        [[SKPaymentQueue defaultQueue] addPayment:payment];
    }
}

唯一的缺点是每次调用restoreCompletedTransactions时,都会弹出窗口要求您输入当前用户的密码。此解决方案可确保每次升级时购买窗口出现的次数不会超过 1 次,但每次您尝试购买其中一项时,所有升级都会恢复。

You can check Array of transactions which will fill after restoreTransaction method and if Array of transactions is empty it means that user download this upgrade for the first time. In another case you'll check all transactions in Array and compare transaction.payment.productIdentifier with needful product identifire. If it doesn't exist add payment in transaction Array.

For Non-Consumable In-App Purchase I used following code:

#define kInAppPurchaseProUpgradeProductId @"upgradeProductId"

//...
//your payment code for all SKPaymentTransactionStates
//...

//method called when BUY button press
-(void)purchaseProUpgrade{
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
//when restore completed delegate method calls 
-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue{

    if([[[SKPaymentQueue defaultQueue] transactions] count]==0)            
        [self addNewPaymentForProductId:kInAppPurchaseProUpgradeProductId];
    else            
        for (SKPaymentTransaction *transaction in [[SKPaymentQueue defaultQueue] transactions]){

            if (![transaction.payment.productIdentifier isEqualToString:kInAppPurchaseProUpgradeProductId]){
                [self addNewPaymentForProductId:kInAppPurchaseProUpgradeProductId];
                break;
            }
        }    
}

-(void)addNewPaymentForProductId:(NSString *)productId{
    if([SKPaymentQueue canMakePayments]){
        SKPayment *payment = [SKPayment paymentWithProductIdentifier:productId];
        [[SKPaymentQueue defaultQueue] addPayment:payment];
    }
}

the only disadvantage is that every time you call restoreCompletedTransactions, window will pop up asking you to enter the password of the current user. This solution ensures that the buy window does not appear more than 1 time for each upgrade but all the upgrades will restore every time when you'll try to buy one of them.

不确定,但也许您可以检查 transaction.originalTransacion 是否存在或是否不同。

Not sure but maybe you can check if transaction.originalTransacion exists or if is different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文