在IOS开发中NSURL的操作及用法有哪些

来源:爱站网时间:2019-04-10编辑:网友分享
NSURL是我们在浏览器上面看到的网址,很多人会觉得奇怪为什么字符串还要在写一个NSURL,相信很多人都会想知道在IOS开发中NSURL的操作及用法有哪些?

  NSURL是我们在浏览器上面看到的网址,很多人会觉得奇怪为什么字符串还要在写一个NSURL,相信很多人都会想知道在IOS开发中NSURL的操作及用法有哪些?
  其实是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程中需要解析出来每个部门,所以封装一个NSURL,操作很方便。
  1.URL
  URL是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址。互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该怎么处理它。
  URL可能包含远程服务器上的资源的位置,本地磁盘上的文件的路径,甚至任意一段编码的数据。
  2.NSURL
  NSURL其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个NSURL呢?
  主要是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程中需要解析出来每个部门,所以封装一个NSURL,操作很方便。
  3.用途
  (1)可以使用URL对象构造URL和访问他们的部分。例如,[myURLscheme]
  (2)对于代表本地文件的url,您也可以直接操作这些文件的属性。例如,修改文件的最后修改日期。
  (3)可以使用url进行网络通信。例如,您可以使用NSURLSessionNSURLConnection,和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的操作及用法有哪些的内容,希望对大家有帮助。

上一篇:iOS开发中之文件目录的访问及管理的基本方法

下一篇:iOS中的圆形头像怎么裁剪

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载