要包裝它,
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
}
此外t o @Lal Krishna的回答,這裏有一個可能有所幫助的鏈接:https://stackoverflow.com/a/4614190/7019821 – Prashant