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

前端 未结 4 1067
無奈伤痛
無奈伤痛 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:27

    add a new scheme for your App

    enter image description here

    as Shown above image...

    then, write a code below on IBAction of your Today Extension

    @IBAction func btnFirstWidgetAction() {
        let url: URL? = URL(string: "schemename://secondViewController")!
        if let appurl = url { self.extensionContext!.open(appurl, completionHandler: nil) }
    }
    
    @IBAction func btnSecondWidgetAction() {
        let url: URL? = URL(string: "schemename://secondViewController")!
        if let appurl = url { self.extensionContext!.open(appurl, completionHandler: nil) }
    }
    
    @IBAction func btnThirdWidgetAction() {
        let url: URL? = URL(string: "schemename://thirdViewController")!
        if let appurl = url { self.extensionContext!.open(appurl, completionHandler: nil) }
    }
    

    than, add method application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) in AppDelegate file and write code to redirect in specific ViewController in this method.

    //call when tap on Extension and get url that is set into a ToadyExtension swift file...
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        let urlPath : String = url.absoluteString
        print(urlPath)
    
        if self.isContainString(urlPath, subString: "firstViewController") {
            //here go to firstViewController view controller
        }
        else if self.isContainString(urlPath, subString: "firstViewController") {
            //here go to secondViewController view controller
        }
        else {
            //here go to thirdViewController view controller
        }
    
        return true
    } 
    

    this method used for check your string is contains as sub string that are given in widget button action. if contain than true otherwise false

     func isContainString(_ string: String, subString: String) -> Bool {
        if (string as NSString).range(of: subString).location != NSNotFound { return true }
        else { return false }
    }
    

提交回复
热议问题