第二次按下“恢复购买”按钮使应用程序崩溃
Posted
技术标签:
【中文标题】第二次按下“恢复购买”按钮使应用程序崩溃【英文标题】:"Restore Purchases" button crashing app on 2nd press 【发布时间】:2019-06-30 22:23:07 【问题描述】:我的恢复购买似乎可以正常工作,只是当您再次按下它时它会导致应用程序崩溃。有解决办法吗?这是我的代码:
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)
编辑:这是第二个视图控制器。它包含允许用户购买产品的按钮。恢复购买按钮位于单独的视图控制器中。我对 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)")
【问题讨论】:
崩溃发生时的日志是什么意思? @Chris 它在应用程序委托中,没有给我。它只是说“线程 1:EXC_BAD_ACCESS(代码=1,地址=0x10)” 该崩溃通常表明您强制解开nil
。添加异常断点以尝试找出崩溃发生的位置。此外,您不应在视图控制器viewDidLoad
中添加事务观察器。您需要在didFinishLaunching
中设置一个事务观察器,以便处理任何可能在之前的运行中挂起的未完成事务。
您缺少很多(可能)相关的 StoreKit 代码。特别是,paymentQueueRestoreCompletedTransactionsFinished
的代码是什么?请发布完整的 SK 代码,也许我可以提供帮助。
您应该只有一个SKPaymentTransactionObserver
,并且您通常会在您的AppDelegate
中创建它。您仍然需要确定实际崩溃的线路。
【参考方案1】:
固定。
override func viewDidDisappear(_ animated: Bool)
SKPaymentQueue.default().remove(self)
【讨论】:
以上是关于第二次按下“恢复购买”按钮使应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
Windows 10 UWP 应用程序 - 后退按钮仅在第二次按下时有效
Contact Form 7 在第一次提交时不起作用,但在第二次按下 sumbit 按钮时它工作正常