NSURL 裝載URL結構的物件

NSURL 裝載URL結構的物件

NSURL功能主要是要處理輸入的網址內容是否符合RFC 1738的規格合法性,如此一來可以利用這個物件先將網址檢查後再依照NSURL提供的方法取得需要的內容會比自行檢查及處理網址內容資訊來的方便。

URL 結構

URL一開始是由<scheme>:<scheme-specific-part>所組成的,由<scheme>來決定通訊的方式,<scheme-specific-part>決定通訊路徑的結構,中間隔著:冒號,下面由這兩塊來介紹:

scheme

scheme主要是表達用何種的通訊方式,例如:

   ftp                     File Transfer protocol
   http                    Hypertext Transfer Protocol
   gopher                  The Gopher protocol
   mailto                  Electronic mail address
   news                    USENET news
   nntp                    USENET news using NNTP access
   telnet                  Reference to interactive sessions
   wais                    Wide Area Information Servers
   file                    Host-specific file names
   prospero                Prospero Directory Service

當然還可以自行其他的scheme來表示,像iOS內部也有自訂的scheme,可以參照Apple URL Scheme Reference,如果你在網址列輸入tel:1234567890時,它就會啟動播號介面並輸入1234567890號碼,在某些應用下,也可以透過設定檔的方式替自已的APP自訂scheme前綴字,這樣輸入你自訂的sheme也能開啟自已的應用。

資料填完整後就能註冊一個屬於自已應用的scheme,在網址列就能使用該scheme,很像檔案關聯性的感覺。

scheme-specific-part

這部分的內容為溝通的內容,下面的格式為Internet所使用的(http、ftp、telnet…):

//user:password@hostname:port/absolute-path?query

舉個例子:

//guest:guest@tw.yahoo.com/index.html

如果有些資訊未輸入,則會依照通訊方式讓處理的程式補上預設值,比如在網頁(http)通訊方式時未輸入port則內定值是80

NSURL 用法

解析網址

當你要利用NSURL物件將網址解析時,直接在初始化時輸入完整的網址:

//-----------start-----------
    NSURL *urlLink = [NSURL URLWithString:@"http://user:password@cms.35g.tw:80/coding/index.php?page"];

    NSLog(@"absoluteString:%@",[urlLink absoluteString]);
    NSLog(@"host:%@",[urlLink host]);
    NSLog(@"port:%i",[urlLink port].intValue);
    NSLog(@"scheme:%@",[urlLink scheme]);
    NSLog(@"query:%@",[urlLink query]);
//------------end------------

輸出結果:

absoluteString:http://user:password@cms.35g.tw:80/coding/index.php?page
host:cms.35g.tw
port:80
scheme:http
query:page

會依照Internet的溝通格式解析,這樣你要直接取得網址的部分資訊就能快速的得到。

組成完整網址

當你是要使用NSURL物件變動一些網址內容並生成完整的網址表達格式也能利用它完成。

例如要產生http://tw.yahoo.com/file.html時,要使用下面的方式去產生,

//-----------start-----------
NSURL *url = [NSURL URLWithString:@"file.html" relativeToURL:[NSURL URLWithString:@"http://tw.yahoo.com"]];
//------------end------------

如此一來,relativeToURL輸入的內容,你在NSURL方法中就可以取得baseURL的值,例子結果它會是你輸入的httpt://tw.yahoo.com,這樣你就能使用basURL來產生其它的完整網址內容,下面的例子使用NSURL來產生兩個網址:

//-----------start-----------
    NSURL *url = [NSURL URLWithString:@"file.html" relativeToURL:[NSURL URLWithString:@"http://tw.yahoo.com"]];
    NSLog(@"absoluteString = %@", [url absoluteString]);
    NSLog(@"baseURL:%@",[url baseURL]);
    //
    NSURL *url1= [[url baseURL] URLByAppendingPathComponent:@"1234.html"];
    NSLog(@"absoluteString = %@", [url1 absoluteString]);
//------------end------------

輸出結果:

absoluteString = http://tw.yahoo.com/file.html
baseURL:http://tw.yahoo.com
absoluteString = http://tw.yahoo.com/1234.html

不過個人建議這樣產生網址的方式真的很麻煩,但我想這個用意應該是利用你輸入的完整網址後,利用取得的host、scheme當做baseURL,間接的再輸入新的路徑來產生新的網址,像下面這樣:

//-----------start-----------
    NSURL *urlLink = [NSURL URLWithString:@"http://user:password@cms.35g.tw:80/coding/index.php?page"];

    NSLog(@"absoluteString:%@",[urlLink absoluteString]);
    NSLog(@"host:%@",[urlLink host]);
    NSLog(@"port:%i",[urlLink port].intValue);
    NSLog(@"scheme:%@",[urlLink scheme]);
    NSLog(@"query:%@",[urlLink query]);
    //
    NSString *n1 = [NSString stringWithFormat:@"%@://%@",[urlLink scheme],[urlLink host]];
    //取得host及scheme組成xxx://xxx.com.tw
    NSURL *url = [NSURL URLWithString:@"file.html" relativeToURL:[NSURL URLWithString:n1]];

    NSLog(@"absoluteString = %@", [url absoluteString]);
    NSLog(@"baseURL:%@",[url baseURL]);
    //
    NSURL *url1= [[url baseURL] URLByAppendingPathComponent:@"1234.html"];
    NSLog(@"absoluteString = %@", [url1 absoluteString]);
//------------end------------

輸出結果:

absoluteString:http://user:password@cms.35g.tw:80/coding/index.php?page
host:cms.35g.tw
port:80
scheme:http
query:page
absoluteString = http://cms.35g.tw/file.html
baseURL:http://cms.35g.tw
absoluteString = http://cms.35g.tw/1234.html

以上為NSURL的用法,不過NSURL最常用在Framework需要網址時,用NSURL物件來傳遞網址內容,例如NSURLRequest

參考網頁:

URLLoadingSystem

NSURL Class Reference

RFC 1808 – Relative Uniform Resource Locators