Q: How I can detect when this event (currently subscribed) has occurred so that I can process the transaction and grant access to my app.
您通过 Apple 验证检测订阅何时存在(我使用 php 网站代码来执行此操作),您会收到“状态代码”响应,并且可以验证它是代码 21006(订阅已过期)还是其他(我将 0 和 21006 以外的任何内容视为实际错误)。
我做事的方式是将交易细节存储在一个 PLIST 文件中,该文件存储在文档目录中。
您可以向 PLIST 添加额外的字段,例如 expiryDates、布尔标志等。
这样您就有了收据的副本,尽管您应该始终验证它,因为它可能已经过期。
问:在观察者的 paymentQueue:updatedTransactions: 方法中,它以 SKPaymentTransactionStateFailed 的形式通过。如何区分此类故障和其他故障,例如用户按下取消按钮?
在 updatedTransactions 方法中使用 switch 语句来确定不同类型的响应。
例子
-(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
NSString *message = [NSString stringWithFormat:@"Transaction failed with error - %@", error.localizedDescription];
NSLog(@"Error - %@", message);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
NSLog(@"updatedTransactions");
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchasing:
// take action whilst processing payment
break;
case SKPaymentTransactionStatePurchased:
// take action when feature is purchased
break;
case SKPaymentTransactionStateRestored:
// take action to restore the app as if it was purchased
break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code != SKErrorPaymentCancelled)
{
// Do something with the error
} // end if
break;
default:
break;
} // end switch
} // next
TransactionStateFailed 处理失败,尽管我没有为取消编写代码,因为我没有理由在我的应用程序中这样做。
问:我是提交返回的交易还是需要调用 restorePreviousTransactions。
我相信 StoreKit 在内部使用 finishTransaction 方法和 restorePreviousTransaction 方法处理这个问题
IE,
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
完成交易
我希望这有帮助