代码之家  ›  专栏  ›  技术社区  ›  RunLoop

从字符串中提取URL

  •  3
  • RunLoop  · 技术社区  · 14 年前

    我的应用程序接收许多文本字符串,这些字符串可能包含也可能不包含字符串中任何位置的URL。从字符串中提取url的最佳方法是什么?谢谢您。

    2 回复  |  直到 14 年前
        1
  •  3
  •   Peter Hosey    14 年前

    The BSD-licensed AutoHyperlinks framework 提供扫描URL文本并将其返回给您或将其标记为属性字符串中的链接的类。

    我不认为它是为iphone开发的,但是你可以添加预处理器指令来删除任何与appkit相关的代码。扫描和返回接口应该只是工作,一旦你得到它的建设。(确保运行测试。)

    Mike Abdullah wrote a patch for iPhone support. 你可以试试。

        2
  •  8
  •   zonble    14 年前

    如果您正在使用mac应用程序,macosx10.6提供了一个新的api,让您可以使用拼写检查器检测url。你可以这样做。

    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;
    }