iPhone SDK:检查NSURL无效的网址的有效性? [英] iPhone SDK: Check validity of URLs with NSURL not working?

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

问题描述

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

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

由于URLWithString应该返回nil如果URL格式不正确,我认为这样可行,但不是出于某种原因。有人可以告诉我为什么吗?
提前致谢!

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无效的网址的有效性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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