国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > IOS开发之NSURL

IOS开发之NSURL

来源:程序员人生   发布时间:2015-04-18 12:10:14 阅读次数:3433次

1:NSURL初始化方法:

 


  1. NSURL *url=[NSURL URLWithString:@"http://www.baidu.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 学习 )



  1. 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"];     
  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⑶1 18:22:20.904 SmallDemoList[5473:11603] 12131232  
  2. 2012-03⑶1 18:22:20.907 SmallDemoList[5473:11603] Scheme: http  
  3. 2012-03⑶1 18:22:20.907 SmallDemoList[5473:11603] Host: www.baidu.com  
  4. 2012-03⑶1 18:22:20.907 SmallDemoList[5473:11603] Port: (null 
  5. 2012-03⑶1 18:22:20.907 SmallDemoList[5473:11603] Path: /s  
  6. 2012-03⑶1 18:22:20.907 SmallDemoList[5473:11603] Relative path: /s  
  7. 2012-03⑶1 18:22:20.907 SmallDemoList[5473:11603] Path components as array:  
  8.     "/" 
  9.      
  10.  
  11. 2012-03⑶1 18:22:20.916 SmallDemoList[5473:11603] Parameter string(null 
  12. 2012-03⑶1 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⑶1 18:22:20.917 SmallDemoList[5473:11603] Fragment: (null 
  14. 2012-03⑶1 18:22:20.917 SmallDemoList[5473:11603] User: (null 
  15. 2012-03⑶1 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⑷5FF-AA2C⑼59E8A65ADB4/SmallDemoList.app/KindEditor/examples/simple.html

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生