[BS-27] 创建NSURL的几个方法的区别

创建NSURL的几个方法的区别

 
 
URL的基本格式 = 协议://主机地址/路径

URLPath的区别

* URL:统一资源定位符,格式协议+主机名称+路径”   例如:[NSURL URLWithString:@"http://www.baidu.com"];

   本地文件URL字符串格式 file:///Users/wz/Desktop/main.c        //本地文件主机地址是localhost,可以省略,故有3/

* Path:就是简单的路径,没有协议,没有主机地址      例如: Users/wz/Desktop/main.c

  NSString *path = [[NSBundle mainBundle] pathForResource:@"buyao.wav" ofType:nil];

    //发送网络请求一般用第1种
    //NSURL *url1 = [NSURL URLWithString:@“http://www.baidu.com”];
    NSURL *url1 = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];//有问题,打印中间有省略号

    //获取本地文件URL一般用2,3两种方法
    NSURL *url2 = [NSURL fileURLWithPath:path];
    NSURL *url3 = [[NSBundle mainBundle] URLForResource:@"buyao.wav" withExtension:nil];

    NSLog(@"\n%@,\n%@,\n%@,\n%@",path,url1,url2,url3);

//打印结果:
/Users/wz/Library/Developer/CoreSimulator/Devices/879D7E35-BE50-4620-97E1-D1023FE13CEA/data/Containers/Bundle/Application/E9404FC7-DC64-42A0-B445-34D61480D620/音效、音乐、流媒体音乐播放.app/buyao.wav,

/Users/wz/Library/Developer/CoreSimulator/Devices/879D7E35-BE50-4620-97E1-D1023FE13CEA/data/Containers/Bundle/Appl ... uyao.wav,

file:///Users/wz/Library/Developer/CoreSimulator/Devices/879D7E35-BE50-4620-97E1-D1023FE13CEA/data/Containers/Bundle/Application/E9404FC7-DC64-42A0-B445-34D61480D620/%E9%9F%B3%E6%95%88%E3%80%81%E9%9F%B3%E4%B9%90%E3%80%81%E6%B5%81%E5%AA%92%E4%BD%93%E9%9F%B3%E4%B9%90%E6%92%AD%E6%94%BE.app/buyao.wav,

file:///Users/wz/Library/Developer/CoreSimulator/Devices/879D7E35-BE50-4620-97E1-D1023FE13CEA/data/Containers/Bundle/Application/E9404FC7-DC64-42A0-B445-34D61480D620/%E9%9F%B3%E6%95%88%E3%80%81%E9%9F%B3%E4%B9%90%E3%80%81%E6%B5%81%E5%AA%92%E4%BD%93%E9%9F%B3%E4%B9%90%E6%92%AD%E6%94%BE.app/buyao.wav

 

iOS开发,NSURL后面跟的字符串可以是随便网址的字符串吗?是不是上个网站把地址粘贴上去就行?

NSURL *URL = [NSURL URLWithString:@"http://www.baidu.com"];

如果你的URL中有中文或者不合法的字符,那么以上方法返回的URL就会为nil。只有在格式规范的条件下才会创建一个有效的NSURL类型。

原因参见官方文档:

Parameters

This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’.    Note that ‘%’ escapes are translated via UTF-8.   The URL string with which to initialize the NSURL object. Must be a URL that conforms to RFC 2396. This method parses URLString according to RFCs 1738 and 1808. 文档里提到string 的格式必须复合RFC 2396,解析按照RFC 1738和1808来进行解析。

Return Value

An NSURL object initialized with URLString. If the URL string was malformed or nil, returns nil.

//不合法的URL

NSURL *url = [NSURL URLWithString:@"http://192.168.0.118:8080/homelife/mobile/??estate!loadPropertyPhone.action?propertyCompanyId=3"]; 

//对其进行转码后,就OK了

NSURL *url = [NSURL URLWithString:[@"http://192.168.0.118:8080/homelife/mobile/estate!loadPropertyPhone.action?propertyCompanyId=3"  stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding] ]; 

 

 

关于NSURL的使用注意事项:

1、 [NSURL URLWithString:urlString],urlString的类型必须正确,必须以http或者https开头,在一次项目中因为在http前面多了个空格,导致字符串转化为NSURL类型时为空。

2、在使用ASI发送请求时,相同信息可以放在http头部统一处理,这样就可以在每次请求中加入相同的参数

3、使用 [NSURL URLWithString:urlString]导致返回的url为nil,因为Return Value An NSURL object initialized with URLString. If the string was malformed, returns nil. Discussion This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.

所以使用

urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

通过utf-8编码,来规避一些特殊字符 url = [NSURL URLWithString:urlStr];

4.如果加载本地的文件,尽量使用 url = [NSURL fileURLWithPath:path]; 该方法会自动对path中的汉字进行转码。

file:///Users/wz/Library/Developer/CoreSimulator/Devices/879D7E35-BE50-4620-97E1-D1023FE13CEA/data/Containers/Bundle/Application/5535F35E-0714-4A6F-8FE8-86C67FF594DA/%E9%9F%B3%E6%95%88%E3%80%81%E9%9F%B3%E4%B9%90%E3%80%81%E6%B5%81%E5%AA%92%E4%BD%93%E9%9F%B3%E4%B9%90%E6%92%AD%E6%94%BE.app/buyao.wav,

 

 

 

 

posted @ 2016-06-04 22:23  stevenwuzheng  阅读(1443)  评论(0编辑  收藏  举报