SwiftUI视图中的StoreKit委托/可观察对象

时间:2020-09-16 12:17:28

标签: ios swiftui storekit

恢复购买后,需要禁用视图中的恢复按钮。

我有一个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
        }
    }

}

1 个答案:

答案 0 :(得分:1)

要在不相关的组件和现有组件之间进行通信,可以使用自定义的通知

  1. 为您的通知创建名称:
extension IAPManager {
    static let proNotification = Notification.Name(rawValue: "proNotification")
}
  1. 在恢复交易的地方,发送通知,通知所有订阅者 Pro 已恢复:
case .restored: 
    print("restored")
    SKPaymentQueue.default().finishTransaction($0)
    NotificationCenter.default.post(name: IAPManager.proNotification, object: nil, userInfo: nil)
  1. 现在您需要一种观察该通知的方法:
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
            }
    }
}