添加和删除事务队列观察器 - 正确的方法?
Adding & Removing Transaction Queue Observer - The Correct Way?
关于应用内购买......我参考了这个技术说明:https://developer.apple.com/library/ios/technotes/tn2387/_index.html
它声明我们应该在AppDelegate
文件中添加didFinishLaunchingWithOptions
中的事务观察器。并且我们应该删除 AppDelegate
的 applicationWillTerminate
中的事务观察器。
这与我读过的许多教程(最新的)不一致,并且与许多关于这个问题的线程(也是最近的)不一致。
我很困惑。苹果显然是'king of the heap'。所以我应该采用技术说明的方向,在 didFinishLaunchingWithOptions
中添加事务队列观察器并在 applicationWillTerminate
?
中删除它
有人可以再澄清一下吗?
在应用程序启动时添加观察者很有用,因为有时应用程序会在购买流程中退出或者互联网可能出现故障(教程没有考虑这种情况,因为它们更具体地解释了教程)
如果一个事务被中断,它将在您注册观察者的下一个事务开始之前完成。为避免这种情况,您可以在应用程序启动时附加它,OS 将在启动时更新上次待处理交易,这将提供更好的用户流。
你问:
It states that we should add the transaction observer in didFinishLaunchingWithOptions
in the AppDelegate file. And that we should remove the transaction observer in the applicationWillTerminate
of the AppDelegate
.
This is not in keeping with many tutorials that I have read ...
不,这样添加没有错。正如技术说明所说,“在启动时添加您的应用程序的观察者可确保它在您的应用程序的所有启动期间持续存在,从而使您的应用程序能够接收所有支付队列通知。”
如果您有一些建议反对这种做法的参考资料,请编辑您的问题并与我们分享具体参考资料,我们可以专门对此发表评论 link。
在评论中,您稍后提问:
I will have to include all the relevant delegate methods in the AppDelegate
also?
有几个选项。例如,您可以为此实例化一个专用对象。因此:
let paymentTransactionObserver = PaymentTransactionObserver()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
SKPaymentQueue.default().add(paymentTransactionObserver)
return true
}
func applicationWillTerminate(_ application: UIApplication) {
SKPaymentQueue.default().remove(paymentTransactionObserver)
}
其中:
class PaymentTransactionObserver: NSObject, SKPaymentTransactionObserver {
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { ... }
func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) { ... }
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) { ... }
func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) { ... }
func paymentQueue(_ queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) { ... }
}
或者,您也可以将其直接添加到您的 AppDelegate
中(如 Setting Up the Transaction Observer for the Payment Queue). but if you do so, you might want add protocol conformance with an extension 中所述,以便将这些相关方法清晰地组合在一起,例如:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
SKPaymentQueue.default().addTransactionObserver(self)
return true
}
func applicationWillTerminate(_ application: UIApplication) {
SKPaymentQueue.default().remove(self)
}
和
extension AppDelegate: SKPaymentTransactionObserver {
// the `SKPaymentTransactionObserver` methods here
}
请参阅 previous revision of this answer 了解 Swift 2 版本。
关于应用内购买......我参考了这个技术说明:https://developer.apple.com/library/ios/technotes/tn2387/_index.html
它声明我们应该在AppDelegate
文件中添加didFinishLaunchingWithOptions
中的事务观察器。并且我们应该删除 AppDelegate
的 applicationWillTerminate
中的事务观察器。
这与我读过的许多教程(最新的)不一致,并且与许多关于这个问题的线程(也是最近的)不一致。
我很困惑。苹果显然是'king of the heap'。所以我应该采用技术说明的方向,在 didFinishLaunchingWithOptions
中添加事务队列观察器并在 applicationWillTerminate
?
有人可以再澄清一下吗?
在应用程序启动时添加观察者很有用,因为有时应用程序会在购买流程中退出或者互联网可能出现故障(教程没有考虑这种情况,因为它们更具体地解释了教程)
如果一个事务被中断,它将在您注册观察者的下一个事务开始之前完成。为避免这种情况,您可以在应用程序启动时附加它,OS 将在启动时更新上次待处理交易,这将提供更好的用户流。
你问:
It states that we should add the transaction observer in
didFinishLaunchingWithOptions
in the AppDelegate file. And that we should remove the transaction observer in theapplicationWillTerminate
of theAppDelegate
.This is not in keeping with many tutorials that I have read ...
不,这样添加没有错。正如技术说明所说,“在启动时添加您的应用程序的观察者可确保它在您的应用程序的所有启动期间持续存在,从而使您的应用程序能够接收所有支付队列通知。”
如果您有一些建议反对这种做法的参考资料,请编辑您的问题并与我们分享具体参考资料,我们可以专门对此发表评论 link。
在评论中,您稍后提问:
I will have to include all the relevant delegate methods in the
AppDelegate
also?
有几个选项。例如,您可以为此实例化一个专用对象。因此:
let paymentTransactionObserver = PaymentTransactionObserver()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
SKPaymentQueue.default().add(paymentTransactionObserver)
return true
}
func applicationWillTerminate(_ application: UIApplication) {
SKPaymentQueue.default().remove(paymentTransactionObserver)
}
其中:
class PaymentTransactionObserver: NSObject, SKPaymentTransactionObserver {
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { ... }
func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) { ... }
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) { ... }
func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) { ... }
func paymentQueue(_ queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) { ... }
}
或者,您也可以将其直接添加到您的 AppDelegate
中(如 Setting Up the Transaction Observer for the Payment Queue). but if you do so, you might want add protocol conformance with an extension 中所述,以便将这些相关方法清晰地组合在一起,例如:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
SKPaymentQueue.default().addTransactionObserver(self)
return true
}
func applicationWillTerminate(_ application: UIApplication) {
SKPaymentQueue.default().remove(self)
}
和
extension AppDelegate: SKPaymentTransactionObserver {
// the `SKPaymentTransactionObserver` methods here
}
请参阅 previous revision of this answer 了解 Swift 2 版本。