Open Safari from my Today Extension (widget) within my app

后端 未结 2 622
清歌不尽
清歌不尽 2021-01-07 08:47

I have a Today Extension with a text field. I want to use the contents of the text field as a URL to open a browser within my app.

This is my TodayViewController.swi

2条回答
  •  情话喂你
    2021-01-07 09:04

    You could use @Giuseppe_Lanza solution and parse url that you receive from Today Extension Widget. However, I would show an example where your url have a static components and looking for a path such as https:/www.apple.com/homepod or https:/www.apple.com/iphone based on user's input in the textField:

    1- URL Scheme: myAppName

    2- Add this to open your app with widget

    @IBAction func goButton(_ sender: Any) {
        openApp(widgetText: "\(textBox.text!)")
    }
    
    func openApp(widgetText:String)    {
    
        let str = "myAppName://https://www.apple.com/\(widgetText)"
        let url = URL(string: str)!
        if textBox.hasText == true  {
    
            extensionContext?.open(url, completionHandler: { (success) in
                if (!success) {
                    print("error:  

提交回复
热议问题