iOS进阶不易的地方iOS扩展ios基础

URL 和 NSURL 格式解析

2016-06-03  本文已影响1001人  Laughingg

1. URL 是什么?

统一资源定位符(Uniform Resource Locator,缩写为URL),又叫做网页地址,是互联网上标准的资源的地址(Address)。互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该怎么处理它。

统一资源定位符:

  1. 定位资源
  2. 确定资源出自哪里(可以用什么方式解析)

2. URL 定义格式

注意,Windows 主机不区分 URL 大小写,但是,Unix/Linux 主机区分大小写。
url 的基本定义:

// url 编码格式
foo://example.com:8042/over/there?name=ferret#nose
   \_/ \______________/ \________/\_________/ \__/
    |         |              |         |        |
scheme     authority        path     query   fragment

URL由三部分组成:资源类型、存放资源的主机域名、资源文件名

scheme://host.domain:port/path/filename

scheme - 定义因特网服务的类型。最常见的类型是 http
host - 定义域主机(http 的默认主机是 www)
domain - 定义因特网域名,比如 w3school.com.cn
:port - 定义主机上的端口号(http 的默认端口号是 80)
path - 定义服务器上的路径(如果省略,则文档必须位于网站的根目录中)。
filename - 定义文档/资源的名称

常见的 scheme

file       资源是本地计算机上的文件。格式file:// 
ftp        通过 FTP访问资源。格式 FTP://
gopher     通过 Gopher 协议访问该资源。 
http       通过 HTTP 访问该资源。 格式 HTTP:// 
https      通过安全的 HTTPS 访问该资源。 格式 target=_blank>HTTPS://
mailto     资源为电子邮件地址,通过 SMTP 访问。 格式 mailto:
MMS        通过 支持MMS(流媒体)协议的播放该资源。(代表软件:Windows Media Player)格式 MMS://
ed2k       通过 支持ed2k(专用下载链接)协议的P2P软件访问该资源。(代表软件:电驴) 格式 ed2k://
Flashget   通过 支持Flashget:(专用下载链接)协议的P2P软件访问该资源。(代表软件:快车) 格式 Flashget://
thunder    通过 支持thunder(专用下载链接)协议的P2P软件访问该资源。(代表软件:迅雷) 格式 thunder://
news       通过 NNTP 访问该资源。 

常见的URL中定位和标识的服务或文件:

http:        文件在WEB服务器上.
file:        文件在您自己的局部系统或匿名服务器上
ftp:         文件在FTP服务器上
gopher:      文件在gopher服务器上
wais:        文件在wais服务器上
news:        文件在Usenet服务器上
telnet:      连接到一个支持Telnet远程登录的服务器上

3. NSURL

NSURL 继承自 NSObject, NSURL 就是对统一资源定位符的数据封装。包含的也就是组成 url 的三部分信息。

NSURL相关属性

    public var absoluteString: String { get }
    public var relativeString: String? { get } // The relative portion of a URL.  If baseURL is nil, or if the receiver is itself absolute, this is the same as absoluteString
    @NSCopying public var baseURL: NSURL? { get } // may be nil.
    @NSCopying public var absoluteURL: NSURL { get } // if the receiver is itself absolute, this will return self.
    
    /* Any URL is composed of these two basic pieces.  The full URL would be the concatenation of [myURL scheme], ':', [myURL resourceSpecifier]
     */
    public var scheme: String { get }
    public var resourceSpecifier: String { get }
    
    /* If the URL conforms to rfc 1808 (the most common form of URL), the following accessors will return the various components; otherwise they return nil.  The litmus test for conformance is as recommended in RFC 1808 - whether the first two characters of resourceSpecifier is @"//".  In all cases, they return the component's value after resolving the receiver against its base URL.
     */
    public var host: String? { get }
    @NSCopying public var port: NSNumber? { get }
    public var user: String? { get }
    public var password: String? { get }
    public var path: String? { get }
    public var fragment: String? { get }
    public var parameterString: String? { get }
    public var query: String? { get }
    public var relativePath: String? { get } // The same as path if baseURL is nil
    
    /* Determines if a given URL string's path represents a directory (i.e. the path component in the URL string ends with a '/' character). This does not check the resource the URL refers to.
     */
    @available(iOS 9.0, *)
    public var hasDirectoryPath: Bool { get }
上一篇下一篇

猜你喜欢

热点阅读