Created
October 28, 2017 16:40
-
-
Save StanislavK/4a651e79057c9d4dc23b495afcea8078 to your computer and use it in GitHub Desktop.
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
extension String { | |
/** Get email addresses in a string, discard any other content. */ | |
func emailAddresses() -> [String] { | |
var addresses = [String]() | |
if let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) { | |
let matches = detector.matches(in: self, options: [], range: NSMakeRange(0, self.count)) | |
for match in matches { | |
if let matchURL = match.url, | |
let matchURLComponents = URLComponents(url: matchURL, resolvingAgainstBaseURL: false), | |
matchURLComponents.scheme == "mailto" | |
{ | |
let address = matchURLComponents.path | |
addresses.append(String(address)) | |
} | |
} | |
} | |
return addresses | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment