我的UITextView
中有一个网址链接。在我的iPhone中,有野生动物园和野生动物园。 chrome应用程序。每当我点击URL链接时,我想列出safari和amp; Chrome(以及手机中的所有其他浏览器)处于弹出视图中,以便用户可以选择要打开网址的浏览器。现在它总是打开safari,因为它是我手机中的默认浏览器。有什么办法可以选择浏览器来打开URL吗?我知道有这个代表
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
}
但是如何强制此网址在Chrome或任何其他浏览器中打开?如何在视图中列出所有可用的浏览器?
答案 0 :(得分:2)
将其包起来,
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
}
答案 1 :(得分:1)
目前,我无法找到任何用于列出所有浏览器的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
答案 2 :(得分:1)
我没有尝试,只是检查一下。将 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
}