Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSURL、NSURLComponent #105

Open
ShannonChenCHN opened this issue Feb 2, 2018 · 2 comments
Open

NSURL、NSURLComponent #105

ShannonChenCHN opened this issue Feb 2, 2018 · 2 comments

Comments

@ShannonChenCHN
Copy link
Owner

No description provided.

This was referenced Feb 2, 2018
@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Feb 2, 2018

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Feb 2, 2018

NSURL 和 NSURLComponent

一、 NSURL

1. 简介

  • What is NSURL:
    - represents an URL
  • Usage
    - the location of a resource on a remote server
    - the path of a local file on disk
    - an arbitrary piece of encoded data
    - interapplication communication
  • Structure of a URL
    - An NSURL object is composed of two parts
    - base URL
    - a string that is resolved relative to the base URL
    - Absolute URL and relative URL
    - Structure of a URL:
    最基础的表示法中,URI 由一个 scheme 和一个 hierarchical part 组成,带有 query 和 fragment(后两者非必需):
<scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]

很多像HTTP的协议中会定义有一定的规范结构,例如:username、password、port, 以及hierarchical part 中的 path:

nsurl

以 URL https://johnny:p4ssw0rd@www.example.com:443/script.ext;param=value?query=value#ref 为例,可以分解为以下几部分:

Components Value
scheme https
user johnny
password p4ssw0rd
host www.example.com
port 443
path /script.ext
pathExtension ext
pathComponents ["/", "script.ext"]
parameterString param=value
query query=value
fragment ref
       

二、 NSURLComponent

NSURL 和 NSURLComponents 的不同之处在于,URL component 属性是 readwrite 的。它提供了安全直接的方法来修改URL的各个部分。

另外,NSURLComponents 也有 readwrite 属性对每个 component 进行 [percent-encoded]。

NSURLComponents 的典型应用场景就是 URL 解析,获取其中的参数。

NSString *urlString = @"http://www.example.com/path/index.html?type=animal&name=panda#nose";
        
        NSURLComponents *components = [NSURLComponents componentsWithString:urlString];
        
        NSArray <NSURLQueryItem *> *queryItems = [components queryItems] ?: @[];
        NSMutableDictionary *queryParams = [NSMutableDictionary dictionary];
        for (NSURLQueryItem *item in queryItems) {
            if (item.value == nil) {
                continue;
            }
            
            if (queryParams[item.name] == nil) {
                // first time seeing a param with this name, set it
                queryParams[item.name] = item.value;
            } else if ([queryParams[item.name] isKindOfClass:[NSArray class]]) {
                // already an array of these items, append it
                NSArray *values = (NSArray *)(queryParams[item.name]);
                queryParams[item.name] = [values arrayByAddingObject:item.value];
            } else {
                // existing non-array value for this key, create an array
                id existingValue = queryParams[item.name];
                queryParams[item.name] = @[existingValue, item.value];
            }
        }
        
        NSLog(@"%@", queryParams);

FAQ

Q: How to deal with some special characters in an URL, like chinese?
A: Using percent-encoding.

NSString * encodingString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

If you want to retrieve the original characters in an url,

NSString *str = [model.album_name stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant