Con Swift 3 y 10 I0s, la forma más sencilla de interactuar con los números de teléfono, direcciones URL o direcciones en UITextView
es utilizar UIDataDetectorTypes
. El siguiente código muestra cómo mostrar un número de teléfono en un UITextView
para que el usuario pueda interactuar con él.
import UIKit
class ViewController: UIViewController {
// Link this outlet to a UITextView in your Storyboard
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.text = "+33687654321"
textView.isUserInteractionEnabled = true // default: true
textView.isEditable = false // default: true
textView.isSelectable = true // default: true
textView.dataDetectorTypes = [.phoneNumber]
}
}
Con este código, al hacer clic en el número de teléfono, un UIAlertController
aparecerá.
Como alternativa, puede utilizar NSAttributedString
:
import UIKit
class ViewController: UIViewController {
// Link this outlet to a UITextView in your Storyboard
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let phoneUrl = NSURL(string: "tel:+33687654321")! // "telprompt://+33687654321" also works
let attributes = [NSLinkAttributeName: phoneUrl]
let attributedString = NSAttributedString(string: "phone number", attributes: attributes)
textView.attributedText = attributedString
textView.isUserInteractionEnabled = true // default: true
textView.isEditable = false // default: true
textView.isSelectable = true // default: true
}
}
Con este código, al hacer clic en la cadena con atributos, un UIAlertController
aparecerá.
Sin embargo, es posible que desee realizar alguna acción personalizada en lugar de hacer un pop UIAlertController
cuando se hace clic en un número de teléfono. O puede mantener un UIAlertController
para que aparezca y realice su propia acción personalizada al mismo tiempo.
En ambos casos, deberá hacer que su UIViewController
cumpla con el protocolo UITextViewDelegate
e implemente textView(_:shouldInteractWith:in:interaction:)
. El siguiente código muestra cómo hacerlo.
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
// Link this outlet to a UITextView in your Storyboard
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
textView.text = "+33687654321"
textView.isUserInteractionEnabled = true
textView.isEditable = false
textView.isSelectable = true
textView.dataDetectorTypes = [.phoneNumber]
}
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
/* perform your own custom actions here */
print(URL)
return false // return true if you still want UIAlertController to pop up
}
}
Con este código, al hacer clic en el número de teléfono, sin UIAlertController
se abrirá y ustedes en lugar de obtener la letra siguiente en la consola:
tel: +33687654321
¡Por fin la respuesta real! Idea simple y genial Gracias, funciona Fue hace mucho tiempo, así que lo he logrado sin eso. Todavía puede ayudar en el futuro. Sin embargo, se debe devolver una corrección pequeña, resultado de super en la rama else: return [super openURL: url]; – Vladimir
¡Genial! Hice la corrección sugerida. ¡Aclamaciones! – fsaint
También podría categorizar 'UIApplication' y reemplazar la implementación de openURL. Aunque de esta manera es complicado (pero no imposible) hacer referencia a la implementación original. – mxcl