如何拦截电话号码点击(呼叫按钮)在UITextView中的链接?
问题描述:
是否有回调来识别用户在弹出窗口中点击拨打电话?如何拦截电话号码点击(呼叫按钮)在UITextView中的链接?
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool
上面提到的功能将帮助我找出一个链接被点击在一个UITextView,虽然是有一个特定的回调,以确定是否呼叫被点击或取消点击
答
使用委托方法:
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
print("Phone \(URL)")
return false
}
不要忘记连接textView委托。
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>