Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: open URL in a specific browser tab?

Tags:

ios

swift

With JavaScript, you can open a URL in a specific browser tab.

How do you accomplish the same thing with Swift from an iOS app, i.e., open URL in Safari/Chrome in a specific tab.

The UIApplication open function doesn't list information about the options parameter, which seems like it might let you specify a named tab in which to open the URL.

like image 308
Crashalot Avatar asked Oct 09 '16 22:10

Crashalot


3 Answers

This is how I open links in Google Chrome:

var newLink: String = "http://www.apple.com"
newLink = link.replacingOccurrences(of: "http://", with: "googlechrome://")
if UIApplication.sharedApplication().canOpenURL(NSURL(string: newLink)!) {
UIApplication.sharedApplication().openURL(NSURL(string: newLink)!)
} else {
    let alertController = UIAlertController(title: "Sorry", message: "Google Chrome app is not installed", preferredStyle: .Alert)
    let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(okAction)
    self.presentViewController(alertController, animated: true, completion: nil)
}

Opera Mini:

opera://open-url?url=http://

Opera Touch:

touch-https://

Firefox:

firefox://open-url?url=http://

Dolphin:

dolphin://

Brave:

brave://open-url?url=http://
like image 114
jaytrixz Avatar answered Oct 20 '22 00:10

jaytrixz


Change the protocol detail (HTTP or HTTPS) of the url to googlechrome and the application will open the link in Chrome:

let sUrl = "googlechrome://www.google.com"
UIApplication.shared.openURL(NSURL(string: sUrl) as! URL)
like image 30
Christian Abella Avatar answered Oct 19 '22 23:10

Christian Abella


In the end I just got Google Chrome (on iOS) to do all the donkey-work (it appears willing to save the password for this website for future retrieval). I also submitted in the URL for the HTTPS version of the website and now all appear to work a little better.

if URL(string: fullURL) != nil {
    //let safariVC = SFSafariViewController(url: url)
    //self.present(safariVC, animated: true, completion: nil)
    UIApplication.shared.openURL(NSURL(string: fullURL)! as URL) 
}
like image 37
William Lombard Avatar answered Oct 20 '22 00:10

William Lombard