Created
October 23, 2020 14:52
-
-
Save tkausch/7c5e195b6d88f527ddd9fd46cff9c394 to your computer and use it in GitHub Desktop.
How to detect a URL in a String using NSDataDetector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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