1

我的视图不检查该值是否已更改。你能告诉我为什么或者你能帮助我吗?

您可以在下面看到重要的代码。

我的课

import SwiftUI
import SwiftyStoreKit

class Foo: ObservableObject {
 @Published var Trigger : Bool = false
  
 func VerifyPurchase() { 
      let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: "your-shared-secret")
      SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in
           switch result {
                case .success(let receipt):
                     let productId = "com.musevisions.SwiftyStoreKit.Purchase1"
                     // Verify the purchase of Consumable or NonConsumable
                     let purchaseResult = SwiftyStoreKit.verifyPurchase(
                          productId: productId,
                          inReceipt: receipt) 
                     
                     switch purchaseResult {
                          case .purchased(let receiptItem):
                               print("\(productId) is purchased: \(receiptItem)")
                               
                               self.Trigger.toggle()
                               
                          case .notPurchased:
                               print("The user has never purchased \(productId)")
                               
                               self.Trigger.toggle()
                     }
                case .error(let error):
                     print("Receipt verification failed: \(error)")
                     
                     self.Trigger.toggle()
                     
           }
      }
 } 
} 

在 SceneDelegate 中,我在 sceneDidbecom 的重要代码将值触发为 true,然后如果函数完成,那么我希望触发返回为 false

 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 

      let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

      let foo = Foo()
      let contentView =  ContenView(foo: Foo).environment(\.managedObjectContext, context) 
       
      if let windowScene = scene as? UIWindowScene {
          let window = UIWindow(windowScene: windowScene)
          window.rootViewController = UIHostingController(rootView: contentView)
          self.window = window
          window.makeKeyAndVisible()
      }
 }


 func sceneDidBecomeActive(_ scene: UIScene) {
 let foo = Foo()
 foo.Trigger = true
 foo.VerifyPurchase()
 }

当值发生变化时,我的视图不会更新自我。

struct ContentView: View {
   @ObservedObject var foo: Foo
 
    var body: some View {
      Text(self.foo.Trigger ? "true" : "false")
    }
}
4

1 回答 1

1

SwiftyStoreKit.verifyReceipt是一个异步函数,@Published变量必须在主线程上更新。

在后台DispatchQueue.main.async更改变量时尝试添加:Trigger

SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in
    ...
    DispatchQueue.main.async {
        self.Trigger = false
    }
}

另请注意,Swift 中的变量通常是小写的- 即。trigger而不是Trigger.


您还在项目中使用了两个不同 Foo的实例。

我建议您从 中删除代码func sceneDidBecomeActive(_ scene: UIScene)并将其移至.onAppearContentView

struct ContentView: View {
   @ObservedObject var foo: Foo
 
    var body: some View {
      Text(self.foo.Trigger ? "true" : "false")
        .onAppear {
            foo.Trigger = true
            foo.VerifyPurchase()
        }
    }
}

也代替

Text(self.foo.Trigger ? "true" : "false")

你可以做

Text(String(describing: self.foo.Trigger))
于 2020-09-17T12:20:11.627 回答