欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS开发中NSURL的基本操作及用法详解

程序员文章站 2023-10-29 12:08:40
nsurl其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个nsurl呢,主要是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程...

nsurl其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个nsurl呢,主要是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程中需要解析出来每个部门,所以封装一个nsurl,操作很方便。

1.url

url是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址。互联网上的每个文件都有一个唯一的url,它包含的信息指出文件的位置以及浏览器应该怎么处理它。

url可能包含远程服务器上的资源的位置,本地磁盘上的文件的路径,甚至任意一段编码的数据。

2.nsurl

nsurl其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个nsurl呢?

主要是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程中需要解析出来每个部门,所以封装一个nsurl,操作很方便。

3.用途

(1)可以使用url对象构造url和访问他们的部分。例如,[myurl scheme]
(2)对于代表本地文件的url,您也可以直接操作这些文件的属性。例如,修改文件的最后修改日期。
(3)可以使用url进行网络通信。例如,您可以使用nsurlsession nsurlconnection,和nsurldownload类来访问远程资源的内容。
(4)可以使用url读写本地文件。例如,你可以通过一个本地文件的url,调用stringwithcontentsofurl方法,得到nsstring格式的文件内容。
(5)可以使用url进行通讯。例如:可以用openurl:方法来拨打电话。
(6)可以使用url添加标签。

举例:

nsurl *url = [nsurl urlwithstring:@"http://www.baidu.com/s?tn=baiduhome_pg&bs=nsrul&f=8&rsv_bp=1&rsv_spt=1&wd=nsurl&inputt=2709"]; 
 nslog(@"scheme: %@", [url scheme]); 
 nslog(@"host: %@", [url host]); 
 nslog(@"port: %@", [url port]); 
 nslog(@"path: %@", [url path]); 
 nslog(@"relative path: %@", [url relativepath]); 
 nslog(@"path components as array: %@", [url pathcomponents]); 
 nslog(@"parameter string: %@", [url parameterstring]); 
 nslog(@"query: %@", [url query]); 
 nslog(@"fragment: %@", [url fragment]); 
 nslog(@"user: %@", [url user]); 
 nslog(@"password: %@", [url password]); 

 结果:

2015-12-10 21:53:57.171 [4697:358837] scheme: http
2015-12-10 21:53:57.171 [4697:358837] host: www.baidu.com
2015-12-10 21:53:57.172 [4697:358837] port: (null)
2015-12-10 21:53:57.172 [4697:358837] path: /s
2015-12-10 21:53:57.172 [4697:358837] relative path: /s
2015-12-10 21:53:57.172 [4697:358837] path components as array: (
 "/",
)
2015-12-10 21:53:57.172 [4697:358837] parameter string: (null)
2015-12-10 21:53:57.173 [4697:358837] query: tn=baiduhome_pg&bs=nsrul&f=8&rsv_bp=1&rsv_spt=1&wd=nsurl&inputt=2709
2015-12-10 21:53:57.173 [4697:358837] fragment: (null)
2015-12-10 21:53:57.173 [4697:358837] user: (null)
2015-12-10 21:53:57.173 [4697:358837] password: (null)

ps:nsurl的用法

1:nsurl初始化方法:

nsurl *url=[nsurl urlwithstring:@"http://www.baidu.com?id=1"]; 

2:解决nsurl初始化失败的方法.

将传进来的nsstring 进行 utf8 转码即可.

nsstring *strlocalhtml = @"file:///users/amarishuyi/desktop/my iphone life/webdeveloper/webplug-in/exteditor/datapage/kmqt/ext-htmleditor.html"; 
strlocalhtml = [nsstring stringwithformat:@"%@?value=%@",strlocalhtml,self.txturl.text]; 
strlocalhtml= [strlocalhtml stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; 
nsurl * url=[nsurl urlwithstring:strlocalhtml]; 

3:nsurl 成功初始化后可以获取的参数 (摘自:nsurl 学习 )

nsurl *url = [nsurl urlwithstring: @"http://www.baidu.com/s?tn=baiduhome_pg&bs=nsrul&f=8&rsv_bp=1&rsv_spt=1&wd=nsurl&inputt=2709"];  
nslog(@"scheme: %@", [url scheme]); 
nslog(@"host: %@", [url host]); 
nslog(@"port: %@", [url port]);  
nslog(@"path: %@", [url path]);  
nslog(@"relative path: %@", [url relativepath]); 
nslog(@"path components as array: %@", [url pathcomponents]);   
nslog(@"parameter string: %@", [url parameterstring]);  
nslog(@"query: %@", [url query]);   
nslog(@"fragment: %@", [url fragment]); 
nslog(@"user: %@", [url user]); 
nslog(@"password: %@", [url password]); 

结果如下:

2012-03-31 18:22:20.904 smalldemolist[5473:11603] 12131232 
2012-03-31 18:22:20.907 smalldemolist[5473:11603] scheme: http 
2012-03-31 18:22:20.907 smalldemolist[5473:11603] host: www.baidu.com 
2012-03-31 18:22:20.907 smalldemolist[5473:11603] port: (null) 
2012-03-31 18:22:20.907 smalldemolist[5473:11603] path: /s 
2012-03-31 18:22:20.907 smalldemolist[5473:11603] relative path: /s 
2012-03-31 18:22:20.907 smalldemolist[5473:11603] path components as array: ( 
 "/", 
) 
2012-03-31 18:22:20.916 smalldemolist[5473:11603] parameter string: (null) 
2012-03-31 18:22:20.917 smalldemolist[5473:11603] query: tn=baiduhome_pg&bs=nsrul&f=8&rsv_bp=1&rsv_spt=1&wd=nsurl&inputt=2709 
2012-03-31 18:22:20.917 smalldemolist[5473:11603] fragment: (null) 
2012-03-31 18:22:20.917 smalldemolist[5473:11603] user: (null) 
2012-03-31 18:22:20.917 smalldemolist[5473:11603] password: (null)

4:根据文件名称和文件后缀获取程序包内容文件的路径

nsurl *urlkindeditor = [[nsbundlemainbundle] urlforresource:@"simple"withextension:@"html"subdirectory:@"kindeditor/examples"]; 

urlforresource:文件名称
withextension:文件后缀
subdirectory:在程序包中的哪个子目录中寻找.

如果没有找到将会返回nil
找到后返回如下路径: file://localhost/users/amarishuyi/library/application%20support/iphone%20simulator/5.1/applications/fb0cdabc-d0e2-45ff-aa2c-959e8a65adb4/smalldemolist.app/kindeditor/examples/simple.html

以上内容是小编给大家分享的ios开发中nsurl的基本操作及用法详解,希望大家喜欢。