Crashalot
Crashalot

Reputation: 34523

Swift: open URL in a specific browser tab?

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.

Upvotes: 7

Views: 12047

Answers (3)

jaytrixz
jaytrixz

Reputation: 4079

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://

Upvotes: 13

William Lombard
William Lombard

Reputation: 347

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) 
}

Upvotes: 2

Christian Abella
Christian Abella

Reputation: 5797

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)

Upvotes: 4

Related Questions