我的 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/