参考In app Purchases ...我参考此技术说明:https://developer.apple.com/library/ios/technotes/tn2387/_index.html
它声明我们应该在didFinishLaunchingWithOptions
文件的AppDelegate
中添加事务观察器。我们应该删除applicationWillTerminate
的{{1}}中的交易观察者。
这与我所阅读的许多教程(当前的教程)不一致,并且与此问题的许多主题(最近也是如此)不一致。
我很困惑。苹果显然是“堆积之王”。所以我应该采用技术说明的方向并在AppDelegate
中添加事务队列观察器并在didFinishLaunchingWithOptions
删除它?
有人可以再详细说明一下吗?提前谢谢。
答案 0 :(得分:14)
你问:
它声明我们应该在AppDelegate文件的
didFinishLaunchingWithOptions
中添加事务观察器。我们应该删除applicationWillTerminate
的{{1}}中的交易观察者。这与我读过的许多教程不一致......
不,以这种方式添加它没有任何问题。正如技术说明所述,“在发布时添加应用程序的观察者可确保在应用程序的所有启动过程中保持它,从而允许您的应用程序接收所有支付队列通知。”
如果您有一些建议反对该做法,请编辑您的问题并与我们分享具体的参考,我们可以专门评论该链接。
在评论中,您后来问过:
我还必须在
AppDelegate
中包含所有相关的委托方法吗?
有几个选择。例如,您可以为此实例化一个专用对象。在Swift 3中:
AppDelegate
或者在Swift 2中:
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)
}
用,在Swift 3中:
let paymentTransactionObserver = PaymentTransactionObserver()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
SKPaymentQueue.defaultQueue().addTransactionObserver(paymentTransactionObserver)
return true
}
func applicationWillTerminate(application: UIApplication) {
SKPaymentQueue.defaultQueue().removeTransactionObserver(paymentTransactionObserver)
}
或者在Swift 2中:
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]) { ... }
}
或者,您也可以将其直接添加到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: NSError) { ... }
func paymentQueue(queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) { ... }
}
,但如果您这样做,则可能需要add protocol conformance with an extension,以便将这些相关方法干净地组合在一起,例如在Swift 3:
AppDelegate
或者在Swift 2中:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
SKPaymentQueue.default().addTransactionObserver(self)
return true
}
func applicationWillTerminate(_ application: UIApplication) {
SKPaymentQueue.default().remove(self)
}
和
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
return true
}
func applicationWillTerminate(application: UIApplication) {
SKPaymentQueue.defaultQueue().removeTransactionObserver(self)
}
答案 1 :(得分:5)
有用的是在应用程序启动时添加观察者,因为有时候应用程序在购买流程中退出或者互联网出现故障(这些情况不会被教程考虑在内,因为它们更具体地解释了教程)
如果事务中断,则在您注册观察者的下一个事务开始之前,它不会完成。为避免这种情况,您将其附加到应用程序启动时,操作系统将在启动时使用上次挂起的事务进行更新,这将为用户提供更好的流量。