问题描述
我在应用程序中有很多1次购买IAP。用户可以很好地购买它们。
I have lots of 1 time purchase IAPs inside of my application. Users can purchase them just fine.
我的问题是我正在与Flurry集成以跟踪实际购买而不是恢复购买,但我的 SKPaymentTransaction
的 transactionState
总是返回 SKPaymentTransactionStatePurchased
而不是 SKPaymentTransactionStateRestored
。
My problem is that I am integrating with Flurry to track real purchases versus just a restoration of a purchase, but my SKPaymentTransaction
's transactionState
always comes back as SKPaymentTransactionStatePurchased
rather than SKPaymentTransactionStateRestored
.
显然 SKPaymentTransactionStateRestored
仅在 - (void)restoreCompletedTransactions $ c $时调用c>,但我什么时候打电话给这个方法?
Apparently SKPaymentTransactionStateRestored
is only called when - (void)restoreCompletedTransactions
, but when do I call this method?
我的想法是购买应该是这样的:1)用户选择产品,2)询问用户是否愿意购买X数量的产品。 3)服务器检查用户之前是否购买过,以及是否通过设置 SKPaymentTransactionStateRestored
恢复用户。否则,处理交易并设置 SKPaymentTransactionStatePurchased
。显然这是错误的,我想调用 - (void)restoreCompletedTransactions
介于两者之间???
My thought process is that a purchase should go like this: 1) User selects product, 2) User is asked if they would like to purchase product for X amount. 3) Server checks if the user has purchased before, and if they have restore it by setting SKPaymentTransactionStateRestored
. Otherwise, process transaction and set SKPaymentTransactionStatePurchased
. Apparently this is wrong and I am suppose to call - (void)restoreCompletedTransactions
somewhere in between???
谢谢,
将
推荐答案
编辑:
最初我发布了一个非常长的,不需要的方法来获得我需要的东西,但正如你将在下面看到的,Matt帮助我弄清楚我正在寻找的变量for。
Originally I had posted a very long, unneeded method to get what I needed done, but as you will see below, Matt helped me figure out the variable I was looking for.
举个例子,假设用户以前购买过我的应用程序,购买了所有可用的非消耗性IAP,然后删除了应用程序。当用户重新安装应用程序时,我希望能够确定他们何时再次购买产品,是原始(第一次)购买还是恢复购买?
For an example, let's imagine a user previously purchased my app, bought all of the non-consumable IAP's available, then deleted the app. When the user reinstalls the app, I want to be able to determine when they go to "purchase" the products again, will it be an original (first time) purchase, or a restoration purchase?
我已经实施了恢复所有购买按钮,但是假设用户忽略/看不到它,并尝试选择之前购买的产品。
I have implemented a "Restore all purchases" button, but let's say the user ignores/does not see it, and tries to select a product they have purchased before.
与正常购买一样,我会执行以下操作:
As with a normal purchase, I do the following:
if ([SKPaymentQueue canMakePayments])
{
self.productRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:productID]];
self.productRequest.delegate = self;
[self.productRequest start];
}
else
{
//error message here
}
用户登录iTunes帐户后,应用程序会让他们知道他们已经购买了这个帐户,现在它将被恢复。将调用以下委托方法:
After the user has logged into their iTunes account, the App will let them know they have already purchased this and it will now be restored. The following delegate method will be called:
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
if(transaction.originalTransaction)
{
NSLog(@"Just restoring the transaction");
}
else
{
NSLog(@"First time transaction");
}
break;
}
default:
break;
}
}
}
无论交易是否恢复或第一次购买, transaction.transactionState
将等于 SKPaymentTransactionStatePurchased
。
No matter if the transaction was a restoration or a first time purchase, transaction.transactionState
is going to be equal to SKPaymentTransactionStatePurchased
.
现在,从这一点开始,我们如何确定购买是原始购买还是恢复购买?
Now from this point how do we determine if that purchase is an original or restoration purchase?
简单:如上所示,只需查看 transaction.originalTransaction
是否已初始化。根据Apple的说明: //仅在状态为SKPaymentTransactionStateRestored时有效。
Simple: As seen above, just see if transaction.originalTransaction
is initialized. Per Apple's note: // Only valid if state is SKPaymentTransactionStateRestored.
如果 SKPaymentTransaction
's originalTransaction
已初始化,这意味着之前有一笔交易。否则,这笔交易是原创交易!
If the SKPaymentTransaction
's originalTransaction
is initialized, that means that there was a previous transaction. Otherwise, this transaction is an original one!
再一次,感谢Matt指出我正确的方向,并使逻辑更清洁!
Once again, thanks to Matt for pointing me in the right direction, and for making the logic a lot cleaner!
这篇关于iOS StoreKit - 何时调用 - (void)restoreCompletedTransactions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!