美文网首页
iOS技术文档No.19 AppKit_NSURL

iOS技术文档No.19 AppKit_NSURL

作者: 孤独雪域 | 来源:发表于2017-07-24 17:25 被阅读11次

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 *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]);  

打印:

2017-07-24 17:21:52.331 No.19 UIKit_NSURL[19557:1263699] Scheme: http
2017-07-24 17:21:52.331 No.19 UIKit_NSURL[19557:1263699] Host: www.baidu.com
2017-07-24 17:21:52.332 No.19 UIKit_NSURL[19557:1263699] Port: (null)
2017-07-24 17:21:52.332 No.19 UIKit_NSURL[19557:1263699] Path: /s
2017-07-24 17:21:52.333 No.19 UIKit_NSURL[19557:1263699] Relative path: /s
2017-07-24 17:21:52.334 No.19 UIKit_NSURL[19557:1263699] Path components as array: (
    "/",
    s
)
2017-07-24 17:21:52.334 No.19 UIKit_NSURL[19557:1263699] Parameter string: (null)
2017-07-24 17:21:52.334 No.19 UIKit_NSURL[19557:1263699] Query: tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709
2017-07-24 17:21:52.335 No.19 UIKit_NSURL[19557:1263699] Fragment: (null)
2017-07-24 17:21:52.335 No.19 UIKit_NSURL[19557:1263699] User: (null)
2017-07-24 17:21:52.335 No.19 UIKit_NSURL[19557:1263699] 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
5:对比两个URL 是否相等**
[url isEqual:[_audioPlayer url]]  

相关文章

网友评论

      本文标题:iOS技术文档No.19 AppKit_NSURL

      本文链接:https://www.haomeiwen.com/subject/wyibkxtx.html