2

My View don't check that's the value has changed. Can you tell me why or can you help me?

You can see below the important code.

My Class

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()
                     
           }
      }
 } 
} 

In SceneDelegate my important code at sceneDidbecom to trigger the value to true and then if the function completed, then I want that's trigger back to 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()
 }

My View that's doesnt update self when the Value has changed.

struct ContentView: View {
   @ObservedObject var foo: Foo
 
    var body: some View {
      Text(self.foo.Trigger ? "true" : "false")
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
Aiiboo
  • 613
  • 2
  • 6
  • 13

1 Answers1

2

SwiftyStoreKit.verifyReceipt is an asynchronous function and @Published variables must be updated on the main thread.

Try adding DispatchQueue.main.async when you change the Trigger variable in the background:

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

Also note that variables in Swift are usually lowercased - ie. trigger instead of Trigger.


You're also using two different Foo instances in your project.

I recommend you remove the code from the func sceneDidBecomeActive(_ scene: UIScene) and move it to the .onAppear in the ContentView:

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

Also instead of

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

you can do

Text(String(describing: self.foo.Trigger))
pawello2222
  • 46,897
  • 22
  • 145
  • 209