我的视图不检查该值是否已更改。你能告诉我为什么或者你能帮助我吗?
您可以在下面看到重要的代码。
我的课
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")
}
}