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

标签 ios swift transactions in-app-purchase

引用应用内购买...我引用了这个技术说明: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/

相关文章:

IOS - 解码 PCM 字节数组

ios - 使用 swift 在运行时添加/删除 UITableView 中的部分

java - Grails,使用 withTransaction 插入大量数据会导致 OutOfMemoryError

php - 为什么这个 select for update 示例有效?

ios - 无法定位和更改 CloudKit 可发现性消息

ios - 如何在 TableView Cells 中选择和显示特定信息?

ios - WKWebKit View 在 iOS 10 上重叠状态栏,但在 iOS 11 上不重叠

ios - 在Swift中实现UILabel动画效果的最佳方法?

subsonic - SubSonic 的 SimpleRepository 可以参与两种不同对象类型的事务吗?

objective-c - iOS 上的 CoreLocation 仅在一定时间内监视后台的位置变化?