使用委託方法:
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>