我遵循了此guide,在我的应用中设置了应用内购买。我已经做了所有事情,并在App Store Connect中进行了设置。我的问题是,目前实际购买的商品只是ArePagesUnlocked = true
的UserDefaults中的一个布尔值,但是我在产品中实际定义此位置时遇到了麻烦。我了解如何设置和管理购买与还原,但是我在哪里真正将产品定义为在“购买按钮操作”中将ArePagesUnlocked的值从false更改为true的指令?我尝试过,但是如果没有连接,那么用户仍然可以将其解锁。我是否必须单独构建并发送?在研究过程中,我一直感到有些困惑,到目前为止还没有找到明确的答案,因此不胜感激。谢谢。
编辑:代码:
public init(productIds: Set<ProductIdentifier>) {
productIdentifiers = productIds
for productIdentifier in productIds {
let purchased = UserDefaults.standard.bool(forKey: productIdentifier) // this i use to see if purchased
if purchased {
purchasedProductIdentifiers.insert(productIdentifier)
print("Previously purchased: \(productIdentifier)")
} else {
print("Not purchased: \(productIdentifier)")
}
}
super.init()
SKPaymentQueue.default().add(self)
}
}
是否需要从上述值中添加一个单独的UD值?直到重新启动应用程序后,它才知道是否购买了该软件,如何使其立即生效?
extension IAPHelper: SKPaymentTransactionObserver {
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 complete(transaction: SKPaymentTransaction) {
print("complete...")
deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
}
private func restore(transaction: SKPaymentTransaction) {
guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }
print("restore... \(productIdentifier)")
deliverPurchaseNotificationFor(identifier: productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
}
private func fail(transaction: SKPaymentTransaction) {
print("fail...")
if let transactionError = transaction.error as NSError?,
let localizedDescription = transaction.error?.localizedDescription,
transactionError.code != SKError.paymentCancelled.rawValue {
print("Transaction Error: \(localizedDescription)")
}
SKPaymentQueue.default().finishTransaction(transaction)
}
private func deliverPurchaseNotificationFor(identifier: String?) {
guard let identifier = identifier else { return }
purchasedProductIdentifiers.insert(identifier)
UserDefaults.standard.set(true, forKey: identifier)
NotificationCenter.default.post(name: .IAPHelperPurchaseNotification, object: identifier)
}
答案 0 :(得分:0)
通常的策略是,如果用户购买了商品,则在paymentQueue(_:updatedTransactions:)
中写入用户默认设置。