BUNDLE: Jumpstart your iOS development with the Swift Power Pack – now available! >>

How to detect a URL in a String using NSDataDetector

Available from iOS 4.0

The NSDataDetector class makes it easy to detect URLs inside a string using just a few lines of code. This example loops through all URLs in a string, printing each one out:

let input = "This is a test with the URL https://www.hackingwithswift.com to be detected."
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 {
    let url = (input as NSString).substring(with: match.range)

Note that it takes a shortcut by casting to an NSString so that substring(with:) can be used – this is because the matches returned by the data detector have an NSRange rather than a Swift string range. If you want to do things the "official way" you should use this helper extension:

extension NSRange {
    func range(for str: String) -> Range<String.Index>? {
        guard location != NSNotFound else { return nil }

        guard let fromUTFIndex = str.utf16.index(str.utf16.startIndex, offsetBy: location, limitedBy: str.utf16.endIndex) else { return nil }
        guard let toUTFIndex = str.utf16.index(fromUTFIndex, offsetBy: length, limitedBy: str.utf16.endIndex) else { return nil }
        guard let fromIndex = String.Index(fromUTFIndex, within: str) else { return nil }
        guard let toIndex = String.Index(toUTFIndex, within: str) else { return nil }

        return fromIndex ..< toIndex

With that in place you can now pull out a match like this:

let url = input.substring(with: match.range.range(for: input)!)

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Hacking with watchOS

Transfer your Swift skills to watchOS the easy way, and learn to build real-world apps in the process!

Click here to visit the Hacking with Swift store >>