gpt4 book ai didi

ios - 消耗品 - 此应用内购买已被购买。它将免费恢复

转载 作者:行者123 更新时间:2023-12-01 16:22:00 28 4
gpt4 key购买 nike

在我的实时应用程序上,用户不断收到有关消费品的错误消息。这是非常随机的错误,很少发生。

此应用内购买已被购买。它将免费恢复。

在我的应用程序中,除非应用程序购买过程完成,否则我阻止用户点击“立即购买”按钮。

我已经阅读了以下问题的解决方案

Sandbox trying to restore consumable IAP

My IAP isn't working. Bugs at func Paymentqueue

我在我的代码中的两个地方有 SKPaymentQueue.default().add() ,如下所示。我还为每个 transactionState 调用 SKPaymentQueue.default().finishTransaction(transaction)。

谁能让我知道我还需要检查什么来解决这个问题?

open class IAPHelper: NSObject  {

// Callback
var purchaseStatusBlock: ((IAPHandlerAlertType, String, NSData) -> Void)?
var purchaseFailed: ((SKPaymentTransaction) -> Void)?

private let productIdentifiers: Set<ProductIdentifier>

private var productsRequest: SKProductsRequest?

private var productsRequestCompletionHandler: ProductsRequestCompletionHandler?

public init(productIds: Set<ProductIdentifier>) {

productIdentifiers = productIds

super.init()

SKPaymentQueue.default().add(self) // #1
}
}

第二个是
extension IAPHelper {

public func requestProducts(_ completionHandler: @escaping ProductsRequestCompletionHandler) {
productsRequest?.cancel()
productsRequestCompletionHandler = completionHandler

productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
productsRequest!.delegate = self
productsRequest!.start()
}

public func buyProduct(_ product: SKProduct, vc: UIViewController) {
let viewController = vc as! PurchaseViewController

let payment = SKPayment(product: product)

SKPaymentQueue.default().add(payment) // #2
}
}

交易
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) {
deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)

let receiptURL = Bundle.main.appStoreReceiptURL
let receipt = NSData(contentsOf: receiptURL!)
if (receipt == nil) {
// No local receipt -- handle the error
let alert = UIAlertController(title: "Purchase Error", message: "No local receipt", preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default) { (action) in

}
alert.addAction(okAction)

return
}

// Callback
purchaseStatusBlock?(.purchased, transaction.payment.productIdentifier, receipt!)

SKPaymentQueue.default().finishTransaction(transaction)
}

private func fail(transaction: SKPaymentTransaction) {
if let transactionError = transaction.error as NSError?,
let localizedDescription = transaction.error?.localizedDescription,
transactionError.code != SKError.paymentCancelled.rawValue {
}

// Callback
purchaseFailed?(transaction)

SKPaymentQueue.default().finishTransaction(transaction)
}

private func restore(transaction: SKPaymentTransaction) {
guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }

deliverPurchaseNotificationFor(identifier: productIdentifier)
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)
}
}

最佳答案

我们有一个类似的问题困扰了我们很长时间......

当用户发起购买然后在交易完全处理之前断开互联网连接或终止应用程序时,他们将被收取费用但即使在恢复后也不会收到 IAP 内容

解决方案

关注 Apple's best practices ,并添加事务观察者 在应用启动时 👍

在你的情况下

你应该删除:

SKPaymentQueue.default().add(self)  // #1

来自您的 IAPHelper.init方法。

而是在 AppDelegate 中添加观察者:
class AppDelegate: UIResponder, UIApplicationDelegate {

let iapHelper = IAPHelper()

func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

SKPaymentQueue.default().add(iapHelper)
}

然后在你需要的 ViewController 中,你可以访问 iapHelper使用:
let iapHelper = (UIApplication.shared.delegate as! AppDelegate).iapHelper

关于ios - 消耗品 - 此应用内购买已被购买。它将免费恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55090294/

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