How can I check if UISearchBar.text contains a URL? I thought of doing something like this:
if (searchBar.text == NSTextCheckingType.Link) {
}
With Swift 3, you can use NSDataDetector
. NSDataDetector
has an initializer called init(types:). init(types:)
has the following declaration:
init(types checkingTypes: NSTextCheckingTypes) throws
Initializes and returns a data detector instance.
In order to create a data detector that finds urls, you have to pass NSTextCheckingResult.CheckingType.link as the parameter for init(types:)
.
NSDataDetector
and NSRegularExpression
's enumerateMatches(in:options:range:using:)
methodAs a subclass of NSRegularExpression
, NSDataDetector
has a method called enumerateMatches(in:options:range:using:). enumerateMatches(in:options:range:using:)
has the following declaration:
func enumerateMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange, using block: (NSTextCheckingResult?, NSRegularExpression.MatchingFlags, UnsafeMutablePointer) -> Void)
Enumerates the string allowing the Block to handle each regular expression match.
The Playground code below shows how to use NSDataDetector
and enumerateMatches(in:options:range:using:)
method in order to detect if a String
contains URL
s:
import Foundation
let testString = " lorem http://www.yahoo.com ipsum google.fr"
do {
let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let range = NSRange(location: 0, length: testString.characters.count)
let block = { (result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer) -> Void in
if let result = result, result.resultType == NSTextCheckingResult.CheckingType.link {
print(result.url)
}
}
detector.enumerateMatches(in: testString, options: [], range: range, using: block)
} catch {
print(error)
}
/*
prints:
Optional(http://www.yahoo.com)
Optional(http://google.fr)
*/
NSDataDetector
and NSRegularExpression
's matches(in:options:range:)
methodAs a subclass of NSRegularExpression
, NSDataDetector
has a method called matches(in:options:range:). matches(in:options:range:)
has the following declaration:
func matches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> [NSTextCheckingResult]
Returns an array containing all the matches of the regular expression in the string.
This is a convenience method that calls
enumerateMatches(in:options:range:using:)
passing the appropriate string, options, and range.
The Playground code below shows how to use NSDataDetector
and matches(in:options:range:)
method in order to detect if a String
contains URL
s:
import Foundation
let testString = " lorem http://www.yahoo.com ipsum google.fr"
do {
let detector = try NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let range = NSRange(location: 0, length: testString.characters.count)
let resultArray = detector.matches(in: testString, options: [], range: range)
for result in resultArray {
if result.resultType == NSTextCheckingResult.CheckingType.link {
print(result.url)
}
}
} catch {
print(error)
}
/*
prints:
Optional(http://www.yahoo.com)
Optional(http://google.fr)
*/