В моем iOS-приложении у меня есть куча сообщений alertController, содержащих текст и один встроенный номер телефона, и я хотел предложить пользователю возможность совершать вызовы по alertControllerAction, и для этого мне нужно иметь возможность извлечь номер телефона из строки динамически, превратить его в URL телефонного номера и позволить старому быстрому парню делать свою работу, и вот что я сделал после того, как проследил за дюжиной туто вокруг NSDataDetector, я придумал эту функцию, которая по какой-то причине всегда возвращает ноль в моем объекте phoneNumberURL. Не могли бы вы, ребята, проверить это и сказать мне, если что-то не так?
Здесь ничего не идет:
private func showsHelpMessage()
{
let title = Bundle.main.localizedString(forKey: "account.help.popup.title",
value: "",
table: AFPConfig.sharedInstance.kLocalizableTable)
let message = Bundle.main.localizedString(forKey: "account.help.popup.message",
value: "",
table: AFPConfig.sharedInstance.kLocalizableTable)
var phoneNumber : String = ""
let detectorType: NSTextCheckingResult.CheckingType = [.phoneNumber]
do
{
let detector = try NSDataDetector(types: detectorType.rawValue)
let phoneNumberDetected = detector.firstMatch(in: message, options: [], range: NSRange(location: 0, length: message.utf16.count))
phoneNumber = (phoneNumberDetected?.phoneNumber)!
phoneNumber = phoneNumber.removeWhitespace() // added this because i noticed the NSURL kept crashing because of the whitespaces between numbers
}
catch
{
phoneNumber = "+33969390215"
}
if let phoneURL = NSURL(string: ("tel://" + phoneNumber))
{
let alertAccessibility = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
alertAccessibility.addAction(UIAlertAction(title: "Appeler ?", style: .destructive, handler: { (action) in
UIApplication.shared.open(phoneURL as URL, options: [:], completionHandler: nil)
}))
alertAccessibility.addAction(UIAlertAction(title: "Annuler", style: UIAlertAction.Style.cancel, handler: nil))
self.present(alertAccessibility, animated: true, completion: nil)
}
}
Заранее спасибо и ура!