• IOS NSURL基本操作-备


    NSURL其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个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.       
    3.     NSLog(@"Scheme: %@", [url scheme]);  
    4.       
    5.     NSLog(@"Host: %@", [url host]);  
    6.       
    7.     NSLog(@"Port: %@", [url port]);  
    8.       
    9.     NSLog(@"Path: %@", [url path]);  
    10.       
    11.     NSLog(@"Relative path: %@", [url relativePath]);  
    12.       
    13.     NSLog(@"Path components as array: %@", [url pathComponents]);  
    14.       
    15.     NSLog(@"Parameter string: %@", [url parameterString]);  
    16.       
    17.     NSLog(@"Query: %@", [url query]);  
    18.       
    19.     NSLog(@"Fragment: %@", [url fragment]);  
    20.       
    21.     NSLog(@"User: %@", [url user]);  
    22.       
    23.     NSLog(@"Password: %@", [url password]);  

    结果:

      1. 2012-08-29 15:52:23.781 NSurl[3560:f803] Scheme: http  
      2. 2012-08-29 15:52:32.793 NSurl[3560:f803] Host: www.baidu.com  
      3. 2012-08-29 15:52:39.102 NSurl[3560:f803] Port: (null)  
      4. 2012-08-29 15:52:42.590 NSurl[3560:f803] Path: /s  
      5. 2012-08-29 15:52:52.516 NSurl[3560:f803] Relative path: /s  
      6. 2012-08-29 15:53:05.576 NSurl[3560:f803] Path components as array: (  
      7.     "/",  
      8.     s  
      9. )  
      10. 2012-08-29 15:53:32.861 NSurl[3560:f803] Parameter string: (null)  
      11. 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  
      12. 2012-08-29 15:53:52.942 NSurl[3560:f803] Fragment: (null)  
      13. 2012-08-29 15:53:54.539 NSurl[3560:f803] User: (null)  
      14. 2012-08-29 15:53:57.808 NSurl[3560:f803] Password: (null)  

    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-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:根据文件名称和文件后缀获取程序包内容文件的路径

    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

  • 相关阅读:
    LearnMoreStudyLess《如何高效学习》斯科特.杨
    Asp.net 生成多个Excel打包zip进行下载(建立在Aspose.Cells.dll生成Excel,建立在ICSharpCode.SharpZipLib.dll打包zip)
    【面经】美团测试1,2,3面,一起来聊聊?
    【python】面试高频:浅拷贝 vs 深拷贝、'==' vs 'is'
    【图解Http 学习摘要】五、HTTPS 中的加密、证书介绍,不一直使用 HTTPS 的原因
    【图解Http 学习摘要】四、HTTP 缺点
    【图解Http 学习摘要】三、HTTP 协议基础、四次挥手
    【图解Http 学习摘要】二、IP,TCP 和 DNS、三次握手
    【图解Http 学习摘要】一、http介绍、TCP/IP 协议族
    【杂谈】关于常见架构的整理,单应用、微服务、SOA、分布式和集群
  • 原文地址:https://www.cnblogs.com/isItOk/p/5228833.html
Copyright © 2020-2023  润新知