Extract links from string optimization
And that is the answer for Swift 5.0
let text = "http://www.google.com. http://www.bla.com"
func checkForUrls(text: String) -> [URL] {
let types: NSTextCheckingResult.CheckingType = .link
do {
let detector = try NSDataDetector(types: types.rawValue)
let matches = detector.matches(in: text, options: .reportCompletion, range: NSMakeRange(0, text.count))
return matches.compactMap({$0.url})
} catch let error {
debugPrint(error.localizedDescription)
}
return []
}
checkForUrls(text: text)
Very helpful thread! Here's an example that worked in Swift 1.2, based on Victor Sigler's answer.
// extract first link (if available) and open it!
let text = "How technology is changing our relationships to each other: http://t.ted.com/mzRtRfX"
let types: NSTextCheckingType = .Link
do {
let detector = try NSDataDetector(types: types.rawValue)
let matches = detector.matchesInString(text, options: .ReportCompletion, range: NSMakeRange(0, text.characters.count))
if matches.count > 0 {
let url = matches[0].URL!
print("Opening URL: \(url)")
UIApplication.sharedApplication().openURL(url)
}
} catch {
// none found or some other issue
print ("error in findAndOpenURL detector")
}
Like AdamPro13 said above using NSDataDetector
you can easily get all the URLs, see it the following code :
let text = "http://www.google.com. http://www.bla.com"
let types: NSTextCheckingType = .Link
var error : NSError?
let detector = NSDataDetector(types: types.rawValue, error: &error)
var matches = detector!.matchesInString(text, options: nil, range: NSMakeRange(0, count(text)))
for match in matches {
println(match.URL!)
}
It outputs :
http://www.google.com
http://www.bla.com
Updated to Swift 2.0
let text = "http://www.google.com. http://www.bla.com"
let types: NSTextCheckingType = .Link
let detector = try? NSDataDetector(types: types.rawValue)
guard let detect = detector else {
return
}
let matches = detect.matchesInString(text, options: .ReportCompletion, range: NSMakeRange(0, text.characters.count))
for match in matches {
print(match.URL!)
}
Remember to use the guard
statement in the above case it must be inside a function or loop.
I hope this help.