NSURL

要了解NSURL,首先要了解URL的基本概念,URL由几个部分组成:scheme,host,port,path,query,fragment,如url = http://zh.wikipedia.org/w/index.php?title=Special:%E9%9A%8F%E6%9C%BA%E9%A1%B5%E9%9D%A2&printable=yes#label,scheme = http,host = zh.wikipedia.org,port = null,path = /w/index.php,query为title=XXX,printable = yes,fragment为label

1:NSURL初始化方法:

1 NSURL *url=[NSURL URLWithString:@"http://www.ubluesky.com?id=1"];

2:解决NSURL初始化失败的相关解决方案.

将传进来的NSString 进行 UTF8 转码即可.

1:针对 URLWithString 初始化失败的解决方案

1 NSString *strLocalHtml = @"file:///Users/amarishuyi/Desktop/My IPhone Life/WebDeveloper/WebPlug-in/ExtEditor/DataPage/KMQT/Ext-HTMLEditor.html";
2 strLocalHtml = [NSString stringWithFormat:@"%@?Value=%@",strLocalHtml,self.txtUrl.text];
3 strLocalHtml= [strLocalHtml stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
4 NSURL * url=[NSURL URLWithString:strLocalHtml];

2:针对 fileURLWithPath 初始化失败的解决方案

1 self.filePathString = [self.filePathString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
2 NSURL *url = [NSURL fileURLWithPath:self.filePathString];

转码成功后 会自动 在字符串左侧添加 “file:///

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

2 NSLog(@"Scheme: %@", [url scheme]);
3 NSLog(@"Host: %@", [url host]);
4 NSLog(@"Port: %@", [url port]);
5 NSLog(@"Path: %@", [url path]);
6 NSLog(@"Relative path: %@", [url relativePath]);
7 NSLog(@"Path components as array: %@", [url pathComponents]);
8 NSLog(@"Parameter string: %@", [url parameterString]);
9 NSLog(@"Query: %@", [url query]);
10 NSLog(@"Fragment: %@", [url fragment]);
11 NSLog(@"User: %@", [url user]);
12 NSLog(@"Password: %@", [url password]);

结果如下:

1 2012-03-31 18:22:20.904 SmallDemoList[5473:11603] 12131232
2 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Scheme: http
3 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Host: www.baidu.com
4 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Port: (null)
5 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Path: /s
6 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Relative path: /s
7 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Path components as array: (
8     "/",
9     s
10 )
11 2012-03-31 18:22:20.916 SmallDemoList[5473:11603] Parameter string: (null)
12 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
13 2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Fragment: (null)
14 2012-03-31 18:22:20.917 SmallDemoList[5473:11603] User: (null)
15 2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Password: (null)

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

1 NSURL *urlKindEditor = [[NSBundlemainBundle]URLForResource:@"simple"withExtension:@"html"subdirectory:@"KindEditor/examples"];
2
3 URLForResource:文件名称
4 withExtension:文件后缀
5 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

5:对比两个URL 是否相等

1 [url isEqual:[_audioPlayer url]]

尽管iPhone不允许同时运行两个应用程序,我们可以从自己的应用程序中启动另一个应用程序,并且可以在应用程序之间共享数据。我们可以使用UIApplication类的openURL:方法从一个应用程序来启动另一个应用程序。例如,要在Safari应用程序中打开Google主页,我们可以编写如下代码:NSURL *url [NSURL URLWithString:@"http://google.com"];  [[UIApplication sharedApplication]openURL:url]; 

 
这里的http://部分叫做URL方案(URL scheme),它表示想要载入的应用程序。
还有几种用于本地iPhone应用程序的URL方案,并且可以使用类似的方式来启动它们。
例如,要启动Mail应用程序(如图3-15所示),我们可以使用:

NSURL *url [NSURL URLWithString:@"mailto:steve@apple.com subject= test"];  [[UIApplication sharedApplication] openURL:url]; 
 
要启动SMS应用程序,我们可以编写如下代码:

NSURL *url [NSURL URLWithString:  -"sms:555-1234"];  [[UIApplication sharedApplication]  -penURL:url]; 
 
要拨打一个电话号码,我们可以使用如下代码:

NSURL *url=[NSURL URLWithString:@"tel://555-1234"];  [[UIApplication sharedApplication]openURL:url]; 
 
要启动Maps应用程序来查找一个披萨店(如图3-16所示),我们使用如下代码:

NSURL *url [NSURL URLWithString:@"http://maps.google.com/maps?q=pizza"];  [[UIApplication sharedApplication]openURL:url]; 
 
我们也可以使用URL方案来启动自己的应用程序:
用一个定制的URL方案来启动应用程序:
1)创建一个新的基于视图的应用程序,将其保存为URLSchemeExample。
2)在Xcode Groups & Files面板中,展开Resource部分,并且选择<app>-Info.plist文件。
3)鼠标右键点击Information Property List键,并点击添加箭头从列表中选择“URL types”(如图3-17所示)。
4)展开Item 1,用鼠标右键点击URL identifier,并且再次选择添加箭头从列表中选择URL Schemes(如图3-18所示)。
103649747 
图3-16   启动Maps应用程序并查找披萨店
103715800 
(点击查看大图)图3-17   添加一个URL类型
103724643 
图3-18   添加一个URL方案
103744741 
图3-19   设置URL方案的名称
5)选择Item 1,并且将其值设置为myapplication(如图3-19所示)。
6)打开URLSchemeExampleView Controller.m,取消对viewDidLoad方法的注释,并且编写如下代码:

[self.view setBackgroundColor:[UIColor redColor]]; 
7)构建并运行应用程序。应该看到一个没有内容的红色屏幕。应用程序此时不会做任何事情,但是通过运行它(安装在iPhone或者模拟器上的应用程序),我们只是注册在步骤5中创建的URL方案(myapplication)。
8)我们可以使用如下代码,从另一个不同的应用程序启动该应用程序:
NSURL *url [NSURL URLWithString:@"myapplication:"];  [[UIApplication sharedApplication] openURL:url];

(转自:http://ubluesky.com/archives/55)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值