美文网首页
Foundation - NSURL路径

Foundation - NSURL路径

作者: baiwulong | 来源:发表于2017-12-14 14:53 被阅读9次

    URL

    URL(统一资源定位符) ,通过这个url来找对对应的资源。

    NSURL

    主要是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程中需要解析出来每个部门,所以苹果封装一个NSURL,操作很方便。

    一个 NSURL 对象代表了一个表示远程服务器资源或者本地文件的 URL。所以在 iOS 的网络请求和操作文件系统的 API 中,很多都需要 NSURL 类型的参数。

    url字符串编码处理:http://www.jianshu.com/p/917cec978530

    URL结构分析

    hierarchical part
            ┌───────────────────┴─────────────────────┐
                        authority               path
            ┌───────────────┴───────────────┐┌───┴────┐
      abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1
      └┬┘   └───────┬───────┘ └────┬────┘ └┬┘           └─────────┬─────────┘ └──┬──┘
    scheme  user information     host     port                  query         fragment
    
      urn:example:mammal:monotreme:echidna
      └┬┘ └────────────┬───────────────┘
    scheme              path
    
    作者:二巷小鞭
    链接:http://www.jianshu.com/p/38f5f53dfbad
    

    NSURL属性

    @property (nullable, readonly, copy) NSString *absoluteString;
    @property (readonly, copy) NSString *relativeString; 
    @property (nullable, readonly, copy) NSURL *baseURL;
    @property (nullable, readonly, copy) NSURL *absoluteURL;
    @property (nullable, readonly, copy) NSString *scheme;
    @property (nullable, readonly, copy) NSString *resourceSpecifier;
    
    @property (nullable, readonly, copy) NSString *host;
    @property (nullable, readonly, copy) NSNumber *port;
    @property (nullable, readonly, copy) NSString *user;
    @property (nullable, readonly, copy) NSString *password;
    @property (nullable, readonly, copy) NSString *path;
    @property (nullable, readonly, copy) NSString *fragment;
    @property (nullable, readonly, copy) NSString *parameterString;
    @property (nullable, readonly, copy) NSString *query;
    @property (nullable, readonly, copy) NSString *relativePath;
    

    代码例子:

    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)  
    
    

    常用方法

    • URLWithString:
    • initWithString:
    • fileURLWithPath:
    • initFileURLWithPath:

    NSURLComponents

    NSURLComponents 是一个用于解析和构建 URL 的类,
    通过 NSURLComponents 可以单独修改 URL 的某一部分,获取某一部分编码后的值。

    创建一个 NSURLComponents 实例

        NSURLComponents *c = [NSURLComponents componentsWithString:@"http://baidu.com:80?a=a&b=b#cc"];
        NSLog(@"URL:%@",c.URL);
        NSLog(@"scheme:%@",c.scheme);
        NSLog(@"user:%@",c.user);
        NSLog(@"password:%@",c.password);
        NSLog(@"host:%@",c.host);
        NSLog(@"port:%@",c.port);
        NSLog(@"path:%@",c.path);
        NSLog(@"query:%@",c.query);
        NSLog(@"fragment:%@",c.fragment);//fragment:cc
        c.fragment = @"ddddd";
        NSLog(@"fragment:%@",c.fragment); //fragment:ddddd
    
        NSLog(@"percentEncodedUser:%@",c.percentEncodedUser);
        NSLog(@"percentEncodedPassword:%@",c.percentEncodedPassword);
        NSLog(@"percentEncodedHost:%@",c.percentEncodedHost);
        NSLog(@"percentEncodedPath:%@",c.percentEncodedPath);
        NSLog(@"percentEncodedQuery:%@",c.percentEncodedQuery);
        NSLog(@"percentEncodedFragment:%@",c.percentEncodedFragment);
    

    参考:
    http://www.jianshu.com/p/38f5f53dfbad

    相关文章

      网友评论

          本文标题:Foundation - NSURL路径

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