Search code examples
iosswiftnsdatadetector

Phone numbers detected using NSDataDetector could not be opened using openURL


I use NSDataDetector to detect phone numbers like this:

func matchesFor(type: NSTextCheckingResult.CheckingType) -> [String] {
    
    do {
        
        let nsstring = self as NSString
        let detector = try NSDataDetector(types: type.rawValue)
        let matches = detector.matches(in: self, options: [], range: NSRange(location: 0, length: nsstring.length))
        
        return matches.map { nsstring.substring(with: $0.range) }
        
    } catch {
        return []
    }
}

and it finds a numbers. But then I try to open that numbers like this:

func makeACall(phoneNumber: String) {
    
    if let url = URL(string: "tel://\(phoneNumber)"), UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.openURL(url)
    }
}

and it doesn't work. Why?

The example I mean is:

+48 576-786-987

Should I do anything with that number to use it in order to call?


Solution

  • Remove the space, this should work: +48576-786-987