IOS NSURL基本操作-备
NSURL其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个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-08-29 15:52:23.781 NSurl[3560:f803] Scheme: http
- 2012-08-29 15:52:32.793 NSurl[3560:f803] Host: www.baidu.com
- 2012-08-29 15:52:39.102 NSurl[3560:f803] Port: (null)
- 2012-08-29 15:52:42.590 NSurl[3560:f803] Path: /s
- 2012-08-29 15:52:52.516 NSurl[3560:f803] Relative path: /s
- 2012-08-29 15:53:05.576 NSurl[3560:f803] Path components as array: (
- "/",
- s
- )
- 2012-08-29 15:53:32.861 NSurl[3560:f803] Parameter string: (null)
- 2012-08-29 15:53:37.528 NSurl[3560:f803] Query: tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709
- 2012-08-29 15:53:52.942 NSurl[3560:f803] Fragment: (null)
- 2012-08-29 15:53:54.539 NSurl[3560:f803] User: (null)
- 2012-08-29 15:53:57.808 NSurl[3560:f803] Password: (null)
1:NSURL初始化方法:
- NSURL *url=[NSURL URLWithString:@"http://www.baidu.com?id=1"];
2:解决NSURL初始化失败的相关解决方案.
将传进来的NSString 进行 UTF8 转码即可.
1:针对 URLWithString 初始化失败的解决方案
- 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];
2:针对 fileURLWithPath 初始化失败的解决方案
- self.filePathString = [self.filePathString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSURL *url = [NSURL fileURLWithPath:self.filePathString];
转码成功后 会自动 在字符串左侧添加 "file:///"
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: (
- "/",
- s
- )
- 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 Support/iPhone Simulator/5.1/Applications/FB0CDABC-D0E2-45FF-AA2C-959E8A65ADB4/SmallDemoList.app/KindEditor/examples/simple.html
IOS NSURL基本操作-备的更多相关文章
- iOS 7设计备忘单
With the release of iOS 7, app designers and developers will need to adjust their visual language to ...
- UE4 ios环境搭建备忘
1.windows.mac安装证书 2.安装xcode .app -- 路径可以拖入 sudo gem install xcodeproj 3.错误处理 Setting up Mono Running ...
- NSURL基本操作示例说明
http://blog.csdn.net/zhibudefeng/article/details/7920686
- [ios] NSURL
NSLog(@“Scheme: %@”, [url scheme]); NSLog(@“Host: %@”, [url host]); NSLog(@“Port: %@”, [url port]); ...
- ORACLE基本操作备忘
通过CMD登录SQLPLUS 的语句 C:\Users\Administrator>sqlplus /nolog SQL> conn sys/pwd as sysdba; 导入导出数据库( ...
- Git命令基本操作备忘
创建Git仓库并上传到远程Git仓库 git init git config user.name "显示的名称" git config user.email "EMAIL ...
- GoBelieve IOS SDK接入备忘
项目配置 在工程target的"Build Settings"中,找到"Linking"的"Other Linker Flags",添加参数 ...
- iOS完全自学手册——[二]Hello World工程
1.前言 写第二篇文章之前,我在想第二篇应该怎么写?后来觉得与其写Objective-C语言的相关语法,不如直接开始写个小项目.语法简单入门即可.因为,即便把语法看的很深到最后还是不一定能做项目,运用 ...
- Effective objective-c 2.0阅读笔记
这本书非常的好,看完后,感触挺深,总结纪录一下,针对ios开发的备忘: 注:分类和原著有些不同,自己总结学习用的,仅供参考. 系统篇: 了解oc起源:继承c,由Smalltalk演化而来.动态语言 ...
随机推荐
- mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案
在连接字符串中 添加设置节点 ConnectionLifeTime(计量单位为 秒).超过设定的连接会话 会被杀死! Connection Lifetime, ConnectionLifeTime ...
- BZOJ2453: 维护队列
2453: 维护队列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 183 Solved: 89[Submit][Status] Descripti ...
- jsonp实现跨域资源访问
平时项目中处理ajax跨域资源请求时,例如www.example2.com上的某个页面要请求www.example1.com的数据,我们使用得较多的是jsonp方式.jsonp通过JavaScript ...
- sdl2.0示例
// gcc -o testDrone2_video testDrone2_video.c -lavcodec -lavformat -lswscale -lSDL2// g++ -o testDro ...
- hackerrank【Lego Blocks】:计数类dp
题目大意: 修一个层数为n,长度为m的墙,每一层可以由长度为1.2.3.4的砖块构成. 每一层都在同一个长度处出现缝隙是方案非法的,问合法的方案数有多少种 思路: 先求出总方案,再减去所有非法的方案数 ...
- Palindrome Pairs 解答
Question Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, ...
- java中的“包”与C#中的“命名空间
原文地址:http://www.cnblogs.com/lidabo/archive/2012/12/15/2819865.html Package vs. Namespace 我们知道,重用性(re ...
- security Export/import
export [-k keychain] [-t type] [-f format] [-w] [-p format] [-P passphrase] [-o outfile] Export one ...
- java byte 16进制转换
整型转16进制: int devIdInt = Integer.parseInt(devId);String devIdString = Integer.toHexString(devIdInt); ...
- Python 日期和时间(转)
Python 日期和时间 Python程序能用很多方式处理日期和时间.转换日期格式是一个常见的例行琐事.Python有一个 time 和 calendar 模组可以帮忙. 什么是Tick? 时间间隔是 ...