Fetch URL from string

My application receives many text strings, which may or may not contain URLs anywhere on the string. What would be the best way to extract a url from a string? Thank.

+3
source share
2 answers

The licensed AutoHyperlinks environment, licensed by BSD, provides classes for scanning text for URLs and either returns them to you or marks them as links in the attribute string.

I don't think it's built out of the box for the iPhone, but you can always add preprocessor directives to cut out any AppKit-dependent code. The scan-and-return interface should only work after it has been created. (Be sure to run the tests.)

Mike Abdullah wrote a patch to support iPhone. You can try this.

+3
source

If you're working on a Mac application, Mac OS X 10.6 offers a new API that allows you to discover URLs using spell checking. You can do it this way.

NSString *s = @"http://example.com"
NSInteger wordCount = 0;
NSOrthography *orthography = nil;
NSArray *checkResults = [[NSSpellChecker sharedSpellChecker] checkString:s range:NSMakeRange(0, [s length]) types:NSTextCheckingTypeLink options:nil inSpellDocumentWithTag:0 orthography:&orthography wordCount:&wordCount];
for (NSTextCheckingResult *result in checkResults) {
    NSRange range = result.range;
    NSURL *URL = result.URL;
}
+8
source

Source: https://habr.com/ru/post/1736908/


All Articles