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
You can do this by using deep linking.
First define a custom URL scheme
Once your app responds to the custom scheme my-app://
you can open your app from your todayViewController.
@IBAction func goButton(_ sender: Any) {
let myAppUrl = URL(string: "my-app://openurl/\(yourURL.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed))")!
extensionContext?.open(myAppUrl, completionHandler: { (success) in
if (!success) {
print("error: failed to open app from Today Extension")
}
})
}
In your app, just like described in the previous link you will have to implement in your app delegate
func application(_ app: UIApplication, open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
in this method, the url will be the one you created in your app extension. Meaning it will be my-app://openurl/{the url with percent escaping}
You will have to parse this url, initialise the view controller that contains the webView and pass the url to be opened.
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: