I'm using NSDataDetector to scan a text for links and then return them. I've got it working for the regular alphabet, but since I live in a country with some additions to the english alphabet I can't the NSDataDetector to detect links containing these characters.
Specifically it's the "åäö" that doesn't work. My code looks like this:
var urlStrings = [String]()
let types: NSTextCheckingType = .Link
do {
let detector = try NSDataDetector(types: types.rawValue)
let matches = detector.matchesInString(self, options: .ReportCompletion, range: NSMakeRange(0, self.characters.count))
for match in matches {
if let url = match.URL?.absoluteString {
urlStrings.append(url)
}
}
return urlStrings
} catch {
return urlStrings
}
Hello I do a little modification to your code and work perfectly for me
func detectLinks() -> [String]
{
let string = "http://www.gåäögle.com/testMasåäö http://www.google.com/testMasåäö http://www.gp.se/livsstil/konsument/vegetariskt-ä4r-inte-alltid-så5-nyttigt-1.1236926"
var urlStrings = [String]()
let types: NSTextCheckingType = .Link
do {
let detector = try NSDataDetector(types: types.rawValue)
let matches = detector.matchesInString(string, options: .ReportCompletion, range: NSMakeRange(0, string.characters.count))
for match in matches {
if let url = match.URL?.absoluteString {
urlStrings.append(url)
}
}
return urlStrings
} catch {
return urlStrings
}
}
Maybe your problem is related to self.characters.count
you can check what is wrong but to me this code works fine, urlStrings contains 3 strings after run this code. I hope this help you
I'm using this code to make links that don't crash if there is special characters in it like: "æøå"
var url = "http://www.someurlwithæøåorsomethingelse.com"
let safeUrl = url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.