0

I have a question. in "class SceneDelegate"

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
   let url = userActivity.webpageURL  
print(url) //result is www.mywebsite.com
}

how can I pass this url to my ViewController???

  • Does this answer your question? https://stackoverflow.com/questions/64162820/swift-ios-how-to-pass-data-from-appdelegate-to-viewcontroller-label – jtbandes Dec 09 '20 at 21:51

1 Answers1

1

It'll depend on the view controller hierarchy you have, but let's say you have a UITapBarController as your root view controller and you want to eventually get to a view controller that's within a navigation controller:

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    let url = userActivity.webpageURL  
    if let windowScene = scene as? UIWindowScene {
        for window in windowScene.windows {
            if let rootViewController = window.rootViewController {
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                if  let yourVC = storyboard.instantiateViewController(withIdentifier: "YourVC") as? YourViewController,
                    let tabBarController = rootViewController as? UITabBarController,
                    let navController = tabBarController.selectedViewController as? UINavigationController {
                    yourVC.data = url
                    navController.pushViewController(yourVC, animated: true)
                }
            }
        }
    }
}

Update

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    let url = userActivity.webpageURL
    if let windowScene = scene as? UIWindowScene {
        for window in windowScene.windows {
            if let rootViewController = window.rootViewController {
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                if let yourVC = storyboard.instantiateViewController(withIdentifier: "EventViewController") as? EventViewController,
                   let navController = rootViewController as? UINavigationController {
                    yourVC.data = url
                    navController.pushViewController(yourVC, animated: true)
                }
            }
        }
    }
}
Kevvv
  • 3,655
  • 10
  • 44
  • 90
  • Hey Kevvv, i adapt this line (let yourVC = storyboard.instantiateViewController(withIdentifier: "EventViewController") as? EventViewController,) EventViewController is my second ViewController but the app opens already the first VC. and the data will not be passed in both cases. (Yes I defined the variable in the VC :) ) – user8987919 Dec 10 '20 at 07:59
  • When you say "second view controller", is it second in a tab bar controller or a navigation controller? What does it show if you do `self.view.window?.rootViewController`? Also, does your `EventViewController` have a storyboard ID named `EventViewController`? – Kevvv Dec 10 '20 at 08:10
  • First I Have a NavigationController (initial) then I have the standard ViewController StoryboardID (ViewController) and a second ViewController named EventViewController with the StoryboardID (EventViewController) if I paste self.view.window?.rootViewController to scene I got the issue "Value of type 'SceneDelegate' has no member 'view'" if I remove "view" it brings no change.I always Langs on my "ViewController" with no Data – user8987919 Dec 10 '20 at 08:25
  • You'll have to paste `self.view.window?.rootViewController` within a view controller's `viewDidLoad` method. – Kevvv Dec 10 '20 at 08:46
  • Then I see the "ViewController" – user8987919 Dec 10 '20 at 08:54
  • I found out that I have a Problem in this lines: let tabBarController = rootViewController as? UITabBarController, let navController = tabBarController.selectedViewController as? UINavigationController but I don't know how I resolve it. The IF statement stop in this Line: tabBarController = rootViewController as? UITabBarController – user8987919 Dec 10 '20 at 08:58