Với Swift 3 và i0S 10, cách đơn giản nhất để tương tác với các số điện thoại, url hoặc địa chỉ trong UITextView
là sử dụng UIDataDetectorTypes
. Mã bên dưới cho biết cách hiển thị số điện thoại trong một số UITextView
để người dùng có thể tương tác với nó.
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]
}
}
Với mã này, khi nhấp vào số điện thoại, UIAlertController
sẽ bật lên.
Là một thay thế, bạn có thể sử dụng 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
}
}
Với mã này, khi nhấp vào chuỗi do, một UIAlertController
sẽ bật lên.
Tuy nhiên, bạn có thể thực hiện một số hành động tùy chỉnh thay vì thực hiện UIAlertController
bật lên khi bạn nhấp vào số điện thoại. Hoặc bạn có thể muốn giữ một số UIAlertController
để bật lên và thực hiện hành động tùy chỉnh của riêng bạn trong cùng một thời điểm.
Trong cả hai trường hợp, bạn sẽ phải thực hiện UIViewController
của mình tuân theo giao thức UITextViewDelegate
và triển khai textView(_:shouldInteractWith:in:interaction:)
. Mã dưới đây cho thấy cách thực hiện.
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
}
}
Với mã này, khi nhấp vào số điện thoại, không có UIAlertController
sẽ bật lên và thay vào đó bạn sẽ nhận được in sau trong giao diện điều khiển của bạn:
tel: +33687654321
Ở câu trả lời thực sự cuối cùng! Ý tưởng đơn giản và thú vị. Cảm ơn, nó hoạt động. Đã lâu lắm rồi, nên tôi đã xoay xở mà không có nó. Vẫn có thể giúp đỡ trong tương lai. Chỉnh sửa nhỏ mặc dù, kết quả từ siêu trong chi nhánh khác nên được trả lại: return [super openURL: url]; – Vladimir
Tuyệt! Tôi đã thực hiện điều chỉnh được đề xuất. Chúc mừng! – fsaint
Bạn cũng có thể phân loại 'UIApplication' và thay thế việc triển khai openURL. Mặc dù theo cách này nó là khó khăn (nhưng không phải không thể) để tham khảo thực hiện ban đầu. – mxcl