Search code examples
iosswifttoday-extension

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


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.swift for my widget

import UIKit
import SafariServices
import NotificationCenter

// This extension to remove the white spaces from what pasteed   
extension String {
func replace(string:String, replacement:String) -> String {
    return self.replacingOccurrences(of: string, with: replacement,             
options: NSString.CompareOptions.literal, range: nil)
}

func removeWhitespace() -> String {
    return self.replace(string: " ", replacement: "")
}
}


class TodayViewController: UIViewController, NCWidgetProviding {

var clearNumber: String?

override func viewDidLoad() {
    super.viewDidLoad()

}


func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
    // Perform any setup necessary in order to update the view.

    // If an error is encountered, use NCUpdateResult.Failed
    // If there's no update required, use NCUpdateResult.NoData
    // If there's an update, use NCUpdateResult.NewData

    completionHandler(NCUpdateResult.newData)
}



@IBOutlet weak var textBox: UITextField!

@IBAction func clearNumber(_ sender: Any) {

    if textBox.hasText == true {
        textBox.text = ""
    }else{
        return
    }

}

@IBAction func pasteNumber(_ sender: Any) {

    if let myString = UIPasteboard.general.string {
        let pasteNumber = myString.removeWhitespace()
        textBox.insertText(pasteNumber)
    }else{
        return
    }
}

@IBAction func goButton(_ sender: Any) {

    let myAppUrl = URL(string: "main-screen:")!
    extensionContext?.open(myAppUrl, completionHandler: { (success) in
        if (!success) {
            print("error: failed to open app from Today Extension")
        }
    })
}

Solution

  • 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:  🧐")
                }
            })
        }
    }
    

    3- AppDelegate

    Define a variable and pass received url to webViewController, will parse url there.

    open var receivedUrl:URL?
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool{
    
        receivedUrl = url
        //You need to alter this navigation to match your app requirement so that you get a reference to your previous view..
        window?.rootViewController?.performSegue(withIdentifier: "toDeepLink", sender: nil)
    }
    

    Make sure to make add an identifier for this segue under the attributes inspector as toDeepLink.

    4- WebView & parsing url Now you can get the receivedUrl like this

        override func viewDidLoad() {
        super.viewDidLoad()
    
        let myAppDelegate = UIApplication.shared.delegate as! AppDelegate
        print("receivedUrl \(myAppDelegate.receivedUrl!)")
        //url Parsing & getting rid off urlScheme
        let urlToLoad = URL(string: "\(myAppDelegate.receivedUrl!.host! + ":" + myAppDelegate.receivedUrl!.path)")!
        print(urlToLoad)
        let urlRequest = URLRequest(url: urlToLoad)
        webView.load(urlRequest)
    }
    

    Else, you need to parse it in a proper way like dictionary to assign dynamic values to respective keys in your dictionary and hence to your url or append "?" to your urlToLoad just before you attempt to append url.query as I did in the webView controller.