Amal T S
Amal T S

Reputation: 3415

Choose Browser while clicking url in UITextView

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?

Upvotes: 0

Views: 2794

Answers (3)

Fangming
Fangming

Reputation: 25260

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
}

Upvotes: 5

Dharma
Dharma

Reputation: 3013

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
 }

Upvotes: 1

Lal Krishna
Lal Krishna

Reputation: 16190

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

Upvotes: 1

Related Questions