gpt4 book ai didi

ios - 如何正确处理iOS应用程序内购买以及如何定义产品?

转载 作者:行者123 更新时间:2023-11-30 11:05:09 24 4
gpt4 key购买 nike

我按照 guide 在我的应用程序中设置了应用内购买。我已完成所有操作并在 App Store Connect 中进行设置。我的问题是,目前的实际购买只是 UserDefaults 中 ArePagesUnlocked = true 的 bool 值,但我在产品中实际定义它的位置时遇到了麻烦。我了解如何设置和管理购买和恢复,但在购买按钮操作上,我实际上在哪里将产品定义为将 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)
}

最佳答案

通常的策略是,如果用户购买,则在您的 paymentQueue(_:updatedTransactions:) 中写入用户默认值。

关于ios - 如何正确处理iOS应用程序内购买以及如何定义产品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52805702/

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