How to open Specific View controller on Widgets/ Today Extension click

前端 未结 4 1034
無奈伤痛
無奈伤痛 2021-01-01 01:09

I am trying to open specific view controller on widgets click , but can not able to open it , i am able to open app using url schema but i want to open specific view control

4条回答
  •  离开以前
    2021-01-01 01:24

    Swift5

    Step1: select project>info>url types>add url scheme

    step2: go to the button action method and use this code

    let tag = 1
            if let url = URL(string: "open://\(tag)")
            {
                self.extensionContext?.open(url, completionHandler: nil)
            }
    

    step 3: welcome you get the control of your host app, jus add this in app delegate method

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
        {
            if url.scheme == "open"
            {
                switch url.host
                {
                case "1":
                  let storyboard = UIStoryboard(name: "Main", bundle: nil)
                      let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
                      self.window?.rootViewController = vc
                      self.window?.makeKeyAndVisible()
                default:
                    break
                }
            }
            return true
        }
    

    Congrats! you open the controller.

提交回复
热议问题