0

I have a URL link in my UITextView. And in my iPhone, there are both safari & chrome apps. Whenever I click on the URL link, I want to list both safari & Chrome (and all other browsers in the phone) in a pop up view so that the user can choose which browser they want to open the url. Now it is always opening safari as it is the default browser in my phone. Is there any way I can choose the browser to open the URL? I know there is this delegate

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
        // Got the url here. how can i force this url to open in chrome
        return true
    }

but how can i force this url to open in chrome or any other browser? And how can I list all available browsers in a view?

Amal T S
  • 3,327
  • 2
  • 24
  • 57
  • 1
    In addition to @Lal Krishna 's answer, here's a link that might help : https://stackoverflow.com/a/4614190/7019821 – Prashant Jul 07 '17 at 06:37

3 Answers3

5

To wrap it up,

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
    let actionSheet = UIAlertController(title: "Where do you want to open it", message: "", preferredStyle: .actionSheet)

    if UIApplication.shared.canOpenURL(URL(string: "http://www.stackoverflow.com")!) {
        actionSheet.addAction(UIAlertAction(title: "Safari", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            UIApplication.shared.open(URL(string: "http://www.stackoverflow.com")!, options: [:], completionHandler: nil)
        }))
    }

    if UIApplication.shared.canOpenURL(URL(string: "googlechrome://www.stackoverflow.com")!) {
        actionSheet.addAction(UIAlertAction(title: "Chrome", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            UIApplication.shared.open(URL(string: "googlechrome://www.stackoverflow.com")!, options: [:], completionHandler: nil)
        }))
    }

    if UIApplication.shared.canOpenURL(URL(string: "firefox://open-url?url=http://www.stackoverflow.com")!) {
        actionSheet.addAction(UIAlertAction(title: "Fire Fox", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            UIApplication.shared.open(URL(string: "firefox://open-url?url=http://www.stackoverflow.com")!, options: [:], completionHandler: nil)
        }))
    }

    if UIApplication.shared.canOpenURL(URL(string: "opera-http://www.stackoverflow.com")!) {
        actionSheet.addAction(UIAlertAction(title: "Opera", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            UIApplication.shared.open(URL(string: "opera-http://www.stackoverflow.com")!, options: [:], completionHandler: nil)
        }))
    }

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(actionSheet, animated: true, completion: nil)

    return false
}
Fangming
  • 24,551
  • 6
  • 100
  • 90
1

Currently I can't find any APIs for listing all browsers.

Instead You can use canOpenURL: method to manually check whether the specific app is installed or not.

- (BOOL)isSafariInstalled {
    return [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"http://google.com"]];
}

- (BOOL)isChromeInstalled {
    return [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"googlechrome://"]];
}

- (BOOL)isOperaInstalled {
    return [[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"opera-http://google.com"]];
}

For more about Chrome - Check Docs

Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
  • Thanks for the reply. I will check this. but I want to list all browser and force open it. I will check it & let you know – Amal T S Jul 07 '17 at 06:49
1

i didn't tried but just check it out.Return false to shouldInteractWith delagte method.You this delegate method for your stuffs

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
        // Got the url here. how can i force this url to open in chrome

        //here do your stuffs to show popup or something & based on selection launch url with specific browsers

        return false //to restrict open automatically
 }
Dharma
  • 3,007
  • 3
  • 23
  • 38
  • Thanks for reply. But this will only do not open the url right? I want to list all the browsers & make the URL to open some specific browser other than the default one. – Amal T S Jul 07 '17 at 06:47
  • @Bali you can implement code to showing popup before return false.By choosing browsers load the url – Dharma Jul 07 '17 at 07:03
  • the problem is how can i list the browsers? Basically that was my question. How to fetch the list of available browsers? – Amal T S Jul 07 '17 at 07:08
  • i think there no api to getting all installed app list.you have to filter from major browsers list.[chrome,Firefox,Safari,opera..etc].by testing like here https://stackoverflow.com/questions/31365889/how-to-get-the-default-ios-browser-name – Dharma Jul 07 '17 at 07:15