0

In my iOS application, I have a bunch of alertController messages containing text and one embedded phone number, and I wanted to offer the user the possibility from making the call from an alerControllerAction, and for that I need to be able to extract the phone number from the string dynamically, turn it into a phone number URL and let the old swift guy do its work, and so that's what I did after following about dozen of tuto around NSDataDetector, I came up with this function that for some reason always returns nil in my phoneNumberURL object. Could you guys check it out and tell me if something seems off ?

Here goes nothing :

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)
        }
    }

Thank you in advance, and cheers!

Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31
Luci furr
  • 3
  • 1
  • I think somehow the problem is from here : phoneNumber = (phoneNumberDetected?.phoneNumber)! phoneNumber = phoneNumber.removeWhitespace() I've tried finding alternatives but nothing worked so far so I don't know what's going on – Luci furr Jun 20 '19 at 13:06

2 Answers2

0

Your approach seems ok, but I suspect maybe something about your input data is the real problem. Try experimenting with this in a playground:

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)")
    }
}

If you get a ❌ for anything that you think should be valid, that's your problem.

Also, you've got some odd ( ) in a couple of places:

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

// Concise equivalent
phoneNumber = phoneNumberDetected!.phoneNumber

And:

// 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)
Chris
  • 3,445
  • 3
  • 22
  • 28
  • Yes you're right seems like the problem is in my data the phone numbers are only 4 numbers like "3650" so I guess I have to find another way than NSDataDetector to detect them ? – Luci furr Jun 20 '19 at 14:47
  • A number so short would be hard to distinguish from all sorts of other numbers (house number on a road, distance in meters etc.) so it's no surprise `NSDataDetector` doesn't pick them out. If you can be certain no other numbers will appear, you *could* use a regular expression to extract it. Maybe consider a different approach - rather than try to extract a number from the message, what if the number was used to construct the message? – Chris Jun 20 '19 at 16:04
0

Addressing the problem of extracting numbers which can't be identified as definitely phone numbers (see comment on my other answer):

Rather than try to extract a number from the message and hope that it's a phone number and not a distance or a house number, introduce a placeholder (%d) into the localised string and insert the phone number into the message:

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...

}
Chris
  • 3,445
  • 3
  • 22
  • 28