根据Apple文档,“非消耗品由系统在设备之间进行同步”,或“非消耗品在所有用户的设备上均可用”,或“ StoreKit处理非消耗品的同步和还原过程”。不幸的是,我无法在任何地方找到有关“同步”机制的任何详细信息。 我的应用程序将非消耗性产品的购买状态保存在UserDefaults中,而UserDefaults本质上是本地的。在另一台设备上使用相同的应用程序并按“还原”按钮,我以前没有任何购买。 所以我的问题是: Apple在谈到跨设备同步时意味着什么?为了在设备之间同步购买,需要向我的应用添加什么?
“还原”按钮的代码为:
public func restorePurchases() {
SKPaymentQueue.default().restoreCompletedTransactions()
}
我有一个扩展名:
extension IAPHelper: SKPaymentTransactionObserver {
// Called when a transaction states change
public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch (transaction.transactionState) {
case .purchased:
complete(transaction: transaction)
break
case .failed:
fail(transaction: transaction)
break
case .restored:
restore(transaction: transaction)
break
case .deferred:
break
case .purchasing:
break
}
}
}
还原代码为:
private func restore(transaction: SKPaymentTransaction) {
guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }
print("restore... \(productIdentifier)")
deliverPurchaseNotificationFor(identifier: productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
}
并用于deliveryPurchaseNotificationFor:
private func deliverPurchaseNotificationFor(identifier: String?) {
guard let identifier = identifier else { return }
purchasedProductIdentifiers.insert(identifier)
let defaults = UserDefaults.standard
var currentStatus = defaults.string(forKey:identifier)
currentStatus = "Purchased" // acheté mais pas sauvé
defaults.set(currentStatus, forKey: identifier)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "IAPHelperPurchaseNotification"), object: identifier)
}
答案 0 :(得分:0)
问题很可能是这一行:
guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }
这似乎可能失败。只是写
let productIdentifier = transaction.payment.productIdentifier