1. URL 是什么烹笔?
統(tǒng)一資源定位符(Uniform Resource Locator伙窃,縮寫為URL),又叫做網(wǎng)頁地址浊伙,是互聯(lián)網(wǎng)上標(biāo)準(zhǔn)的資源的地址(Address)撞秋。互聯(lián)網(wǎng)上的每個(gè)文件都有一個(gè)唯一的URL嚣鄙,它包含的信息指出文件的位置以及瀏覽器應(yīng)該怎么處理它吻贿。
統(tǒng)一資源定位符:
- 定位資源
- 確定資源出自哪里(可以用什么方式解析)
2. URL 定義格式
注意,Windows 主機(jī)不區(qū)分 URL 大小寫哑子,但是舅列,Unix/Linux 主機(jī)區(qū)分大小寫。
url 的基本定義:
// url 編碼格式
foo://example.com:8042/over/there?name=ferret#nose
\_/ \______________/ \________/\_________/ \__/
| | | | |
scheme authority path query fragment
URL由三部分組成:資源類型卧蜓、存放資源的主機(jī)域名剧蹂、資源文件名
scheme://host.domain:port/path/filename
scheme - 定義因特網(wǎng)服務(wù)的類型。最常見的類型是 http
host - 定義域主機(jī)(http 的默認(rèn)主機(jī)是 www)
domain - 定義因特網(wǎng)域名烦却,比如 w3school.com.cn
:port - 定義主機(jī)上的端口號(hào)(http 的默認(rèn)端口號(hào)是 80)
path - 定義服務(wù)器上的路徑(如果省略宠叼,則文檔必須位于網(wǎng)站的根目錄中)。
filename - 定義文檔/資源的名稱
常見的 scheme
file 資源是本地計(jì)算機(jī)上的文件其爵。格式file://
ftp 通過 FTP訪問資源冒冬。格式 FTP://
gopher 通過 Gopher 協(xié)議訪問該資源。
http 通過 HTTP 訪問該資源摩渺。 格式 HTTP://
https 通過安全的 HTTPS 訪問該資源简烤。 格式 target=_blank>HTTPS://
mailto 資源為電子郵件地址,通過 SMTP 訪問摇幻。 格式 mailto:
MMS 通過 支持MMS(流媒體)協(xié)議的播放該資源横侦。(代表軟件:Windows Media Player)格式 MMS://
ed2k 通過 支持ed2k(專用下載鏈接)協(xié)議的P2P軟件訪問該資源挥萌。(代表軟件:電驢) 格式 ed2k://
Flashget 通過 支持Flashget:(專用下載鏈接)協(xié)議的P2P軟件訪問該資源。(代表軟件:快車) 格式 Flashget://
thunder 通過 支持thunder(專用下載鏈接)協(xié)議的P2P軟件訪問該資源枉侧。(代表軟件:迅雷) 格式 thunder://
news 通過 NNTP 訪問該資源引瀑。
常見的URL中定位和標(biāo)識(shí)的服務(wù)或文件:
http: 文件在WEB服務(wù)器上.
file: 文件在您自己的局部系統(tǒng)或匿名服務(wù)器上
ftp: 文件在FTP服務(wù)器上
gopher: 文件在gopher服務(wù)器上
wais: 文件在wais服務(wù)器上
news: 文件在Usenet服務(wù)器上
telnet: 連接到一個(gè)支持Telnet遠(yuǎn)程登錄的服務(wù)器上
3. NSURL
NSURL 繼承自 NSObject, NSURL 就是對(duì)統(tǒng)一資源定位符的數(shù)據(jù)封裝。包含的也就是組成 url 的三部分信息榨馁。
NSURL相關(guān)屬性
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 @"http://". 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 }