Skip to content

Instantly share code, notes, and snippets.

@tkausch
Created October 23, 2020 14:52
Show Gist options
  • Save tkausch/7c5e195b6d88f527ddd9fd46cff9c394 to your computer and use it in GitHub Desktop.
Save tkausch/7c5e195b6d88f527ddd9fd46cff9c394 to your computer and use it in GitHub Desktop.
How to detect a URL in a String using NSDataDetector
let input = "My hidden URL https://www.hackingwithswift.com to be detected in a very long string."
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count))
for match in matches {
guard let range = Range(match.range, in: input) else { continue }
let url = input[range]
print(url)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment