如何快速检查 NSString 对象是否是有效的 url?

发布于 2025-01-04 16:06:29 字数 56 浏览 2 评论 0原文

帮我写这样的代码:“如果我的字符串是有效的 URL,请执行以下操作” 是否可以用几串代码来写这个?

Help me to write the code like "if my string is a valid URL do smth"
Is it possible to write this in a couple strings of code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

薄荷梦 2025-01-11 16:06:29

我假设通过 URL,您指的是标识互联网资源位置的字符串。

如果您对输入字符串的格式有所了解,那么为什么不手动检查字符串是否以 http://https:// 或任何其他方案开头你需要。 其他协议,您也可以将它们添加到检查列表中(例如 ftp://mailto:// 等)。



if ([myString hasPrefix:@"http://"] || [myString hasPrefix:@"https://"])
{
    // do something
}

如果您需要 可靠的解决方案并检测任何类型的 URL 方案,那么您应该使用正则表达式。

附带说明一下,NSURL 类旨在表达任何类型的资源位置(不仅仅是互联网资源)。这就是为什么像 img/demo.jpgfile://bla/bla/bla/demo.jpg 这样的字符串可以转换为 NSURL 对象。

但是,根据文档,如果输入字符串不是有效的互联网资源字符串,则 [NSURL URLWithString] 应返回 nil。但实际上并非如此。

I will assume that by URL, you are referring to a string identifying a internet resource location.

If you have an idea about the format of the input string , then why not manually check if the string starts with http://, https:// or any other scheme you need. If you expect other protocols, you can also add them to the check list (e.g. ftp://, mailto://, etc)



if ([myString hasPrefix:@"http://"] || [myString hasPrefix:@"https://"])
{
    // do something
}

If you are looking for a more solid solution and detect any kind of URL scheme, then you should use a regular expression.

As a side note, the NSURL class is designed to express any kind of resource location (not just internet resources). That is why, strings like img/demo.jpg or file://bla/bla/bla/demo.jpg can be transformed into NSURL objects.

However, according to the documentation the [NSURL URLWithString] should return nil if the input string is not a valid internet resource string. In practice it doesn't.

咆哮 2025-01-11 16:06:29
+ (BOOL)validateUrlString:(NSString*)urlString
{
    if (!urlString)
    {
        return NO;
    }

    NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];

    NSRange urlStringRange = NSMakeRange(0, [urlString length]);
    NSMatchingOptions matchingOptions = 0;

    if (1 != [linkDetector numberOfMatchesInString:urlString options:matchingOptions range:urlStringRange])
    {
        return NO;
    }

    NSTextCheckingResult *checkingResult = [linkDetector firstMatchInString:urlString options:matchingOptions range:urlStringRange];

    return checkingResult.resultType == NSTextCheckingTypeLink 
        && NSEqualRanges(checkingResult.range, urlStringRange);
}
+ (BOOL)validateUrlString:(NSString*)urlString
{
    if (!urlString)
    {
        return NO;
    }

    NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];

    NSRange urlStringRange = NSMakeRange(0, [urlString length]);
    NSMatchingOptions matchingOptions = 0;

    if (1 != [linkDetector numberOfMatchesInString:urlString options:matchingOptions range:urlStringRange])
    {
        return NO;
    }

    NSTextCheckingResult *checkingResult = [linkDetector firstMatchInString:urlString options:matchingOptions range:urlStringRange];

    return checkingResult.resultType == NSTextCheckingTypeLink 
        && NSEqualRanges(checkingResult.range, urlStringRange);
}
作妖 2025-01-11 16:06:29

我使用了这个解决方案,这显然是比正则表达式检查更好且更简单的检查 -

- (BOOL)isURL:(NSString *)inputString
{
    NSURL *candidateURL = [NSURL URLWithString:inputString];
    return candidateURL && candidateURL.scheme && candidateURL.host;
}

I used this solution which is apparently a better and less complex check than a Regex check -

- (BOOL)isURL:(NSString *)inputString
{
    NSURL *candidateURL = [NSURL URLWithString:inputString];
    return candidateURL && candidateURL.scheme && candidateURL.host;
}
青衫负雪 2025-01-11 16:06:29

尝试用它创建 NSUrl,并查看它是否返回非零结果。

Try to create NSUrl with it, and see if it returns non-nil result.

抱猫软卧 2025-01-11 16:06:29
if ([NSURL URLWithString:text]) {
  // valid URL
}
else {
  // invalid URL
}
if ([NSURL URLWithString:text]) {
  // valid URL
}
else {
  // invalid URL
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文