【问题标题】:Choose Browser while clicking url in UITextView在 UITextView 中单击 url 时选择浏览器
【发布时间】:2017-07-07 22:14:24
【问题描述】:

我的UITextView 中有一个 URL 链接。在我的 iPhone 中,有 safari 和 chrome 应用程序。每当我单击 URL 链接时,我想在弹出视图中列出 safari 和 Chrome(以及手机中的所有其他浏览器),以便用户可以选择他们想要打开 url 的浏览器。现在它总是打开 safari,因为它是我手机中的默认浏览器。有什么办法可以选择浏览器打开网址吗?我知道有这个代表

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
    }

但是我怎样才能强制这个 url 在 chrome 或任何其他浏览器中打开?以及如何在一个视图中列出所有可用的浏览器?

【问题讨论】:

标签: ios objective-c swift


【解决方案1】:

总结一下,

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
}

【讨论】:

    【解决方案2】:

    目前我找不到任何列出所有浏览器的 API。

    您可以使用canOpenURL: 方法手动检查是否安装了特定应用程序。

    - (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"]];
    }
    

    有关 Chrome 的更多信息 - 请查看Docs

    【讨论】:

    • 感谢您的回复。我会检查这个。但我想列出所有浏览器并强制打开它。我会检查并告诉你
    【解决方案3】:

    我没试过,只是检查一下。返回 false 到 shouldInteractWith delagte 方法。你这个委托方法为你的东西

    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
     }
    

    【讨论】:

    • 感谢您的回复。但这只会不打开url吧?我想列出所有浏览器并设置 URL 以打开默认浏览器以外的某些特定浏览器。
    • @Bali 您可以实现代码以在返回 false 之前显示弹出窗口。通过选择浏览器加载 url
    • 问题是如何列出浏览器?基本上这是我的问题。如何获取可用浏览器列表?
    • 我认为没有 api 可以获取所有已安装的应用程序列表。您必须从主要浏览器列表中进行过滤。[chrome,Firefox,Safari,opera..etc].通过像这里 stackoverflow.com/questions/31365889/…
    猜你喜欢
    • 1970-01-01
    • 2012-10-04
    • 2011-08-12
    • 2014-04-26
    • 1970-01-01
    • 2016-08-29
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多