0

在我的 iOS 应用程序中,我有一堆包含文本和一个嵌入式电话号码的 alertController 消息,我想为用户提供从 alerControllerAction 拨打电话的可能性,为此我需要能够提取电话号码动态地从字符串中,将其转换为电话号码 URL 并让老 swift 家伙完成它的工作,这就是我在围绕 NSDataDetector 跟随大约十几个 tuto 之后所做的,我想出了这个函数,由于某种原因总是返回 nil在我的 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)
        }
    }

提前谢谢你,干杯!

4

2 回答 2

0

您的方法似乎不错,但我怀疑您的输入数据可能是真正的问题。尝试在操场上进行实验:

import Foundation

enum PhoneNumberDetectionError: Error {
    case nothingDetected
    case noNumberFound
}

func extractPhoneURL(from string: String) throws -> URL? {
    let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.phoneNumber.rawValue)

    guard let detected = detector.firstMatch(in: string, options: [], range: NSRange(location: 0, length: string.utf16.count)) else {
        throw PhoneNumberDetectionError.nothingDetected
    }

    guard let number = detected.phoneNumber else {
        throw PhoneNumberDetectionError.noNumberFound
    }

    let noWhiteSpaces = number.filter { !$0.isWhitespace }

    return URL(string: "tel://\(noWhiteSpaces)")
}

let strings = [
    "This 555–692–7753 is a phone number",
    "This 1 555 692 7753 is a phone number",
    "This 123 is an incomplete phone number",
    "This does not have a phone number",
    "This +1 555 692 7753 is a phone number",
]

strings.forEach {
    do {
        guard let url = try extractPhoneURL(from: $0) else {
            print("❌ '\($0)' failed to make URL")
            return
        }
        print("✅ '\($0)' -> \(url.absoluteString)")
    } catch {
        print("❌ '\($0)' : \(error)")
    }
}

如果你认为任何你认为应该有效的东西都得到了❌,那就是你的问题。

另外,你( )在几个地方有一些奇怪的地方:

// Odd combination of optional unwrap and force-unwrap
phoneNumber = (phoneNumberDetected?.phoneNumber)!

// Concise equivalent
phoneNumber = phoneNumberDetected!.phoneNumber

和:

// The brackets around the string concatenation don't achieve anything
if let phoneURL = NSURL(string: ("tel://" + phoneNumber))

// Better
if let phoneURL = NSURL(string: "tel://" + phoneNumber)
于 2019-06-20T13:43:15.907 回答
0

解决提取无法确定为电话号码的号码的问题(请参阅我的其他答案的评论):

与其尝试从消息中提取号码并希望它是电话号码而不是距离或门牌号码,不如%d在本地化字符串中引入占位符 ( ) 并将电话号码插入消息中:

enum LocalPhoneNumbers {
    case reception = 1000
    case helpdesk = 4567
    // etc.
}

private function showHelpMessage() {
    // "Call the helpdesk on %d"
    let format = Bundle.main.localizedString(forKey: "account.help.popup.message",
                                              value: "",
                                              table: AFPConfig.sharedInstance.kLocalizableTable)

    let number = LocalPhoneNumbers.helpdesk.rawValue
    let message = String(format: format, number)
    let url = URL(string: "tel://\(number)")

    // Code to show alert here...

}
于 2019-06-20T16:17:46.183 回答