Home > Back-end >  Passing information from universal link into view controller
Passing information from universal link into view controller

Time:10-03

I've set up a successful Universal Link, which when opened pushes the user to a 'callback' screen. This Universal Link is called after a successful connection to a third party API. When successful, this this API returns a 'code' which I need to do a bunch of stuff in my app's backend.

At the moment, I can log this code successfully in my scene delegate with the following code:

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        // the url is going to be in userActivity.webpageURL
        
        guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,
                let incomingURL = userActivity.webpageURL,
                let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else {
                return
            }

            // Check for specific URL components that you need.
            guard let params = components.queryItems else {
                return
            }

            if let code = params.first(where: { $0.name == "code" } )?.value {

                print("code = \(code)")
                if (userActivity.webpageURL?.lastPathComponent == "callback") {
                guard let windowScene = (scene as? UIWindowScene) else { return }

                        print(code)
                        self.window = UIWindow(windowScene: windowScene)
                        //self.window =  UIWindow(frame: UIScreen.main.bounds)

                        let storyboard = UIStoryboard(name: "Main", bundle: nil)
                        guard let rootVC = storyboard.instantiateViewController(identifier: "CallbackViewController") as? CallbackViewController else {
                            print("ViewController not found")
                            return
                        }
                        let rootNC = UINavigationController(rootViewController: rootVC)
                        self.window?.rootViewController = rootNC
                        self.window?.makeKeyAndVisible()
            }

            } else {
                print("Code is missing")
                return
            }
        
    }

This code is accessed as the incoming URLs queryItems, and can be logged successfully.

The next step I need to take is accessing this code inside of my view controller. To do this, I have attempted to access the scene delegate like so inside of viewDidLoad:

override func viewDidLoad() {
        
        if let sceneDelegate = self.view.window?.windowScene?.delegate as? SceneDelegate {
          let variableToAccess = sceneDelegate.code
          print(variableToAccess)
        }
    }

However, code apparently doesn't exist. Does anyone know what I'm doing wrong here?

CodePudding user response:

Instead of try to reach back to your scene delegate from the CallbackViewController, you can set it at the time that you instantiate the CallbackViewController.

Assuming CallbackViewController looks something like this:

class CallbackViewController : UIViewController {
  var code : String?
} 

And then when you instantiate it:

guard let rootVC = storyboard.instantiateViewController(identifier: "CallbackViewController") as? CallbackViewController else {
   print("ViewController not found")
   return
}
rootVC.code = code
  • Related