Trying to access the environment object created inside scene delegate by another scenedelegate function

Published

I have created an environment object in my scendelegate and want to also use it in another function that resides inside my scenedelegate. The code looks as follows:

SceneDelegate.swift

    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
        var window: UIWindow?
        //@EnvironmentObject var data: DataFetcher
        var data = DataFetcher()
    
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
 
            if let windowScene = scene as? UIWindowScene {
                
                let window = UIWindow(windowScene: windowScene)
                let data = (UIApplication.shared.delegate as! AppDelegate).self.data
                window.rootViewController = UIHostingController(rootView: ContentView().environmentObject(data))
                self.window = window
                window.makeKeyAndVisible()
            }
        }
        
        func handleIncomingDynamicLink(_ dynamicLink: DynamicLink){
            guard let url = dynamicLink.url else {
                print("That's weird. My dynamic link object has no url")
                return
            }
            print("Your incoming link parameter is (url.absoluteString)")
            guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
                let queryItems = components.queryItems else {return}
            self.data.linkRecieved = true
            print(data.linkRecieved)
        }
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        if let incomingURL = userActivity.webpageURL {
            print("Incoming URL is (incomingURL)")
            _ = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
                guard error == nil else{
                    print("Found an error! (error!.localizedDescription)")
                    return
                }
                if let dynamicLink = dynamicLink {
                    self.handleIncomingDynamicLink(dynamicLink)
                }
            }
        }
    }
        

The problem is that the environment object’s linkRecieved variable is not changed inside contentview when the handleIncomingDynamicLink function is executed (I have verified the variable is indeed changed to true once the function is run). Any insight in fixing this issue is much appreciated!!!

Source: Ios Questions

Published
Categorised as environmentobject, ios, swiftui, xcode12 Tagged , , ,

Answers

You need to inject that var data which is property, so remove the following line

let window = UIWindow(windowScene: windowScene)
//let data = (UIApplication.shared.delegate as! AppDelegate).self.data // << this one
window.rootViewController = UIHostingController(rootView: 
   ContentView().environmentObject(self.data))   // << inject own property

Nelson Pfeffer

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq