恢复购买后,需要禁用视图中的恢复按钮。
我有一个IAPManager
的{{1}}类,并且运行良好,当我从主体视图调用SKPaymentTransactionObserver
时,我看到了SKPaymentTransactionObserver的print("restored")
。
因此,问题在于从视图中我找不到该事件,因此我无法禁用该按钮,因为已经恢复了购买。
在我的IAPManager中:
SKPaymentQueue.default().restoreCompletedTransactions
在我看来:
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
}
}
}
答案 0 :(得分:1)
要在不相关的组件和现有组件之间进行通信,可以使用自定义的通知。
extension IAPManager {
static let proNotification = Notification.Name(rawValue: "proNotification")
}
case .restored:
print("restored")
SKPaymentQueue.default().finishTransaction($0)
NotificationCenter.default.post(name: IAPManager.proNotification, object: nil, userInfo: nil)
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
}
}
}