ios - 在 UITextView 中单击 url 时选择浏览器

标签 ios objective-c swift

我的 UITextView 中有一个 URL 链接。在我的 iPhone 中,有 safari 和 chrome 应用程序。每当我单击 URL 链接时,我都想在弹出 View 中列出 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 或任何其他浏览器中打开?以及如何在 View 中列出所有可用的浏览器?

最佳答案

总结一下,

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
}

关于ios - 在 UITextView 中单击 url 时选择浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44963668/

相关文章:

c++ - -fvisibility=hidden 编译器未通过调试版本

iphone - 如何为一个属性添加多个实体(iOS,核心数据)?

iphone - 有没有办法在未提交 Ad-Hoc 分发的设备上测试 iOS 应用程序?

iOS:当应用程序未运行时检测蓝牙设备何时断开连接

iOS 相当于 Ruby VCR Cassettes/recording server http response iOS

ios - 使用 navigationController?.pushViewController (Swift 3.0) 获取黑屏

ios - iOS 版 Appium 的代码覆盖率

objective-c - 我如何为这些 WatchKit 并发症创建模型?

swift - Couchbase 接收 channel 空

ios - tableView(_ tableView : UITableView, cellForRowAt indexPath: IndexPath) 没有在 UITableViewController 子类中被调用