Swift 如何禁用用于应用内购买的按钮,而不将其放入另一个线程?
在我的导航栏中,我有一个按钮可以在应用程序中购买东西Swift 如何禁用用于应用内购买的按钮,而不将其放入另一个线程?,swift,multithreading,in-app-purchase,Swift,Multithreading,In App Purchase,在我的导航栏中,我有一个按钮可以在应用程序中购买东西 let acheterSuppressionPub: UIButton! let acheterSuppressionPubButtonItem = UIBarButtonItem(customView: acheterSuppressionPub) acheterSuppressionPub.addTarget(self, action: #selector(ProposerSupprimerPub), for: .touchUpInsid
let acheterSuppressionPub: UIButton!
let acheterSuppressionPubButtonItem = UIBarButtonItem(customView: acheterSuppressionPub)
acheterSuppressionPub.addTarget(self, action: #selector(ProposerSupprimerPub), for: .touchUpInside)
@objc private func ProposerSupprimerPub() {
if self.adsView.isHidden == false {
afficherSupprimerForAllPubAlert()
}
}
在视图控制器中,我有以下内容:
// désactiver les boutons d'achats
self.acheterSuppressionPub.isEnabled = false
self.acheterSuppressionPub.isHidden = true
// Purchase
if(SKPaymentQueue.canMakePayments()) {
print("IAP is enabled, loading")
let productID: NSSet = NSSet(objects:
"com.KingsFit-W.myfood.removeads")
let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
request.delegate = self
request.start()
} else {
print("please enable IAPS")
}
以下是购买产品的功能:
func buyProduct() {
print("Acheter " + unAchat.productIdentifier)
let pay = SKPayment(product: unAchat)
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().add(pay as SKPayment)
}
func removeAds() {
self.adsView.isHidden = true
self.acheterSuppressionPub.isHidden = true
self.acheterSuppressionPub.isEnabled = false
}
以下是出现问题的函数:
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print("product request")
let myProduct = response.products
for product in myProduct {
print("product added")
print(product.productIdentifier)
print(product.localizedTitle)
print(product.localizedDescription)
print(product.price)
listeDesAchats.append(product)
}
/////////////////////////////////////////////////////////////////
self.acheterSuppressionPub.isEnabled = true // it says: -[UIButton setEnabled:] must be used from main thread only
self.acheterSuppressionPub.isHidden = false
///////////////////////////////////////////////////////
}
它说:-[UIButton setEnabled:]只能从主线程使用
以下是协议功能:
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
print("transactions restored")
for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction
let prodID = t.payment.productIdentifier as String
switch prodID {
case "com.KingsFit-W.myfood.removeads":
print("remove ads")
removeAds()
default:
print("IAP not found")
}
}
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("add payment")
for transaction: AnyObject in transactions {
let trans = transaction as! SKPaymentTransaction
print(trans.error as Any)
switch trans.transactionState {
case .purchased:
print("buy ok, unlock IAP HERE")
print(unAchat.productIdentifier)
let prodID = unAchat.productIdentifier
switch prodID {
case "xxxxxxxxx.removeads":
print("remove ads")
removeAds()
default:
print("IAP not found")
}
queue.finishTransaction(trans)
case .failed:
print("buy error")
queue.finishTransaction(trans)
break
default:
print("Default")
break
}
}
}
如何禁用和隐藏acheterSuppressionPub按钮,而不将其放入另一个线程?
我想停用它,以防止用户在网络请求尚未完成时再次按下它。与接口的所有通信必须在主线程上。跳出到主线程与接口对话的标准方式是:
DispatchQueue.main.async {
self.acheterSuppressionPub.isEnabled = true
self.acheterSuppressionPub.isHidden = false
}
同样地:
func removeAds() {
DispatchQueue.main.async {
self.adsView.isHidden = true
self.acheterSuppressionPub.isHidden = true
self.acheterSuppressionPub.isEnabled = false
}
}
func removeAds() {
DispatchQueue.main.async {
self.adsView.isHidden = true
self.acheterSuppressionPub.isHidden = true
self.acheterSuppressionPub.isEnabled = false
}
}