我创建了一个包含textview的屏幕。在textview里面有一个带有电话号码的文本。我启用了数据检测器,因此,当用户触摸数字时,将触发呼叫事件。但是,当我在真实的设备上执行相应的测试时,如果按下电话号码,它将重定向到消息屏幕,而不是重定向到预先标记的屏幕。
您必须实现以下UITextView的委托方法;
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if URL.scheme == "tel" {
let phone = URL.absoluteString.replacingOccurrences(of: "tel:", with: "")
if let callUrl = URL(string: "tel://\(phone)"), UIApplication.shared.canOpenURL(callUrl) {
UIApplication.shared.open(callUrl)
}
}
return true
}
请检查this答案以供参考。
并且不要忘记遵守UITextViewDelegate
class ViewController: UITextViewDelegate {
}