gpt4 book ai didi

ios - 添加和删​​除事务队列观察器 - 正确的方法?

转载 作者:IT王子 更新时间:2023-10-29 05:47:13 26 4
gpt4 key购买 nike

引用应用内购买...我引用了这个技术说明:https://developer.apple.com/library/ios/technotes/tn2387/_index.html

它声明我们应该在 AppDelegate 文件的 didFinishLaunchingWithOptions 中添加事务观察器。并且我们应该在 AppDelegateapplicationWillTerminate 中移除事务观察器。

这与我读过的许多教程(最新的)不一致,并且与许多关于这个问题的线程(也是最近的)不一致。

我很困惑。苹果显然是“堆中之王”。所以我应该采纳技术说明的方向,在didFinishLaunchingWithOptions 中添加事务队列观察器,并在applicationWillTerminate 中删除它?

有人可以再澄清一下吗?

最佳答案

你问:

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 ...

不,这样添加没有错。正如技术说明所说,“在启动时添加您的应用程序的观察者可确保它在您的应用程序的所有启动过程中持续存在,从而使您的应用程序能够接收所有支付队列通知。”

如果您有一些引用资料反对这种做法,请编辑您的问题并与我们分享具体引用资料,我们可以对该链接发表具体评论。

在评论中,您后来问:

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 中所述)。但如果你这样做,你可能想要 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 版本。

关于ios - 添加和删​​除事务队列观察器 - 正确的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36774163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com