gpt4 book ai didi

ios - 如何拦截 UITextView 中链接上的电话号码单击(调用按钮)?

转载 作者:搜寻专家 更新时间:2023-10-31 22:20:36 24 4
gpt4 key购买 nike

当我们在 TextView 中点击一个数字时,是否有一个回调来识别用户何时点击弹出窗口中的调用

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool

上面提到的函数将帮助我识别已在 UITextView 中单击了一个链接,而是否有特定的回调来识别是否单击了 CallCancel点击

最佳答案

使用委托(delegate)方法:

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
print("Phone \(URL)")
return false
}

不要忘记连接 textView 委托(delegate)。

self.textView.delegate = self

然后你可以添加一个自定义的 UIAlertController 来调用或取消。

编辑:

这是完整的代码:

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {

if (URL.scheme == "tel"){
let phoneNumber = URL.absoluteString.stringByReplacingOccurrencesOfString("tel:", withString: "")
let alert = UIAlertController(title: phoneNumber, message: nil, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Call", style: .Default, handler: { (alert) in
if UIApplication.sharedApplication().canOpenURL(URL) {
UIApplication.sharedApplication().openURL(URL)
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (alert) in
print("User Canceld")
}))
presentViewController(alert, animated: true, completion: nil)
return false
}

return true
}

最后一件事,在您的 info.plist 中添加:

<key>LSApplicationQueriesSchemes</key>
<array>
<string>tel</string>
</array>

关于ios - 如何拦截 UITextView 中链接上的电话号码单击(调用按钮)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39001479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com