如何使用 UITextview 在 ios swift 5 中拨打电话?

Mad*_*ala 1 xcode uitextview swift5

我有带电话号码的 UITextView。在 ios Swift 5 中单击 UITextView 中的电话号码时如何拨打电话。

shr*_*a11 5

TextView 提供一种委托方法来获取任何检测器的点击事件,如电话号码、链接。

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
}
Run Code Online (Sandbox Code Playgroud)

也不要忘记这一点。

textView.dataDetectorTypes = [.phoneNumber]
textView.delegate = self
Run Code Online (Sandbox Code Playgroud)