SwiftUI视图中的StoreKit委托/可观察对象 [英] StoreKit Delegate / Observable object in SwiftUI View
问题描述
恢复购买后,需要禁用视图中的恢复按钮.
When I restored my purchases, I need to disabled the restore button in the view.
我有带有 SKPaymentTransactionObserver
的 IAPManager
类,并且工作正常,我看到SKPaymentTransactionObserver的 print("resteded")
从我的主要视图调用 SKPaymentQueue.default().restoreCompletedTransactions
.
I have IAPManager
class with SKPaymentTransactionObserver
, and it works fine, I saw the print("restored")
of SKPaymentTransactionObserver when I call SKPaymentQueue.default().restoreCompletedTransactions
from my principal view.
因此,问题在于从视图中我找不到该事件,因此我无法禁用该按钮,因为已经恢复了购买.
So, then the problem is that from the view I do not find out about that event and therefore I cannot disable the button, since the purchase has already been restored.
在我的IAPManager中:
In my IAPManager:
func restore(withUsername username: String? = nil) {
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions(withApplicationUsername: username)
}
extension IAPManager: SKPaymentTransactionObserver {
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
transactions.forEach {
switch $0.transactionState {
case .purchasing: ()
print("purchasing")
case .deferred: ()
print("deferred")
case .restored: SKPaymentQueue.default().finishTransaction($0)
print("restaured")
UserDefaults.standard.set(true, forKey: "pro")
case .failed: SKPaymentQueue.default().finishTransaction($0)
print("failed")
UserDefaults.standard.set(false, forKey: "pro")
case .purchased: SKPaymentQueue.default().finishTransaction($0)
print("purchased")
UserDefaults.standard.set(true, forKey: "pro")
@unknown default:
break
}
}
}
在我看来:
Text("Available purchases").font(.title3).foregroundColor(.green)
.onTapGesture {
IAPManager.shared.getProducts()
}.padding()
Spacer()
Button(action: {
let purchased = UserDefaults.standard.bool(forKey: "pro")
if !purchased {
print("Going to try restore")
IAPManager.shared.restore()
} else {
print("restored!")
}
}){
Text((buyed ? "Restored" : "Restore" ))
.fontWeight(.bold)
.font(.none)
.padding(6)
.cornerRadius(40)
.foregroundColor(.blue)
.overlay(
RoundedRectangle(cornerRadius: 40)
.stroke(Color.blue, lineWidth: 0.8)
)
}.buttonStyle(PlainButtonStyle())
.disabled( UserDefaults.standard.bool(forKey: "pro"))
.padding()
推荐答案
要在不相关的组件和已经存在的组件之间进行通信,可以使用自定义的通知.
For communication between unrelated and already existing components you can use custom Notifications.
- 为您的通知创建名称:
extension IAPManager {
static let proNotification = Notification.Name(rawValue: "proNotification")
}
- 在恢复交易的地方,发送通知,通知所有订户 Pro 已恢复:
case .restored:
print("restored")
SKPaymentQueue.default().finishTransaction($0)
NotificationCenter.default.post(name: IAPManager.proNotification, object: nil, userInfo: nil)
- 现在您需要一种观察此通知的方法::
- Now you need a way to observe this notification::
struct ContentView: View {
@State var isPro = false
var body: some View {
Text(isPro ? "pro user" : "standard user")
.onReceive(NotificationCenter.default.publisher(for: IAPManager.proNotification)) { _ in
self.isPro = true
}
}
}
这篇关于SwiftUI视图中的StoreKit委托/可观察对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!