gpt4 book ai didi

ios - "Restore Purchases"按钮在第二次按下时崩溃应用程序

转载 作者:行者123 更新时间:2023-11-28 13:51:26 25 4
gpt4 key购买 nike

我的恢复购买似乎工作正常,但当您第二次按下它时应用程序崩溃了。有解决办法吗?这是我的代码:

import UIKit
import StoreKit

class SettingsVC: UIViewController, SKPaymentTransactionObserver {

@IBOutlet weak var restorePurchaseButton: UIButton!
@IBOutlet weak var privacyPolicyButton: UIButton!
@IBOutlet weak var termsofUseButton: UIButton!
@IBOutlet weak var unlockButton: UIButton!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
SKPaymentQueue.default().add(self)
checkUnlockButton()


}

@IBAction func RestoreBtnClicked(_ sender: Any) {

SKPaymentQueue.default().restoreCompletedTransactions()
restorePurchaseButton.isEnabled = false
}


func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions{
if transaction.transactionState == .restored{
UserDefaults.standard.set(true, forKey: "payment")
SKPaymentQueue.default().finishTransaction(transaction)

}
}
}

编辑这是第二个 View Controller 。它包含允许用户购买产品的按钮。恢复购买按钮位于单独的 View Controller 中。我是 Swift 的新手,所以请尽量简化你的回答,哈哈。

import Foundation
import UIKit
import StoreKit

class UnlockContentVC: UIViewController, SKPaymentTransactionObserver {

@IBOutlet weak var removeAds: UIButton!
@IBOutlet weak var noThanks: UIButton!

override func viewDidLoad() {
//removeAds.layer.cornerRadius = 10

SKPaymentQueue.default().add(self)

removeAds.layer.cornerRadius = removeAds.frame.height / 2

}



@IBAction func dismissModal(_ sender: Any) {

navigationController?.popViewController(animated: true)

dismiss(animated: true, completion: nil)

}

@IBAction func OnClickUlockBtn(_ sender: Any) {
if SKPaymentQueue.canMakePayments(){
//can make payments
let paymentRequest = SKMutablePayment()
paymentRequest.productIdentifier = productID
SKPaymentQueue.default().add(paymentRequest)

}else{
//can't make payments
print("Device can not make payments...")
}

}


func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions{
if transaction.transactionState == SKPaymentTransactionState.purchased{
//User Payment Successfull
UserDefaults.standard.set(true, forKey: "payment")
print("payment was successfull")
SKPaymentQueue.default().finishTransaction(transaction)
}else if transaction.transactionState == .failed{
//User Payment failed
print("transaction failed...")
SKPaymentQueue.default().finishTransaction(transaction)
if let error = transaction.error {
print("\(error.localizedDescription)")
}
}
}
}

}

最佳答案

已修复。

override func viewDidDisappear(_ animated: Bool) {
SKPaymentQueue.default().remove(self)
}

关于ios - "Restore Purchases"按钮在第二次按下时崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54562545/

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