iPhone SDK:使用 NSURL 检查 URL 的有效性不起作用? [英] iPhone SDK: Check validity of URLs with NSURL not working?

查看:26
本文介绍了iPhone SDK:使用 NSURL 检查 URL 的有效性不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查给定的 URL 是否有效,我正在这样做:

I'm trying to check if a given URL is valid and i'm doing it like this:

- (BOOL)urlIsValid:(NSString *)address {
    NSURL *testURL = [NSURL URLWithString:address];
    if (testURL == nil) {
        return NO;
    }
    else {
        return YES;
    }
}

由于如果 URL 格式错误,URLWithString"应该返回nil",我认为这会起作用,但由于某种原因它不起作用.有人可以告诉我为什么吗?提前致谢!

Since "URLWithString" is supposed to return "nil" if the URL is malformed I thought this would work but it doesn't for some reason. Could someone please tell me why? Thanks in advance!

推荐答案

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"yourstring"]];
bool valid = [NSURLConnection canHandleRequest:req];

这篇关于iPhone SDK:使用 NSURL 检查 URL 的有效性不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆