IOS開發中NSURL的基本操作及用法詳解_IOS

來源:互聯網
上載者:User

NSURL其實就是我們在瀏覽器上看到的網站地址,這不就是一個字串麼,為什麼還要在寫一個NSURL呢,主要是因為網站地址的字串都比較複雜,包括很多請求參數,這樣在請求過程中需要解析出來每個部門,所以封裝一個NSURL,操作很方便。

1.URL

URL是對可以從互連網上得到的資源的位置和存取方法的一種簡潔的表示,是互連網上標準資源的地址。互連網上的每個檔案都有一個唯一的URL,它包含的資訊指出檔案的位置以及瀏覽器應該怎麼處理它。

URL可能包含遠程伺服器上的資源的位置,本地磁碟上的檔案的路徑,甚至任意一段編碼的資料。

2.NSURL

NSURL其實就是我們在瀏覽器上看到的網站地址,這不就是一個字串麼,為什麼還要在寫一個NSURL呢?

主要是因為網站地址的字串都比較複雜,包括很多請求參數,這樣在請求過程中需要解析出來每個部門,所以封裝一個NSURL,操作很方便。

3.用途

(1)可以使用URL物件建構URL和訪問他們的部分。例如,[myURL scheme]
(2)對於代表本地檔案的url,您也可以直接操作這些檔案的屬性。例如,修改檔案的最後修改日期。
(3)可以使用url進行網路通訊。例如,您可以使用NSURLSession NSURLConnection,和NSURLDownload類來訪問遠端資源的內容。
(4)可以使用url讀寫本地檔案。例如,你可以通過一個本地檔案的URL,調用stringWithContentsOfURL方法,得到NSString格式的檔案內容。
(5)可以使用url進行通訊。例如:可以用openURL:方法來撥打到電話。
(6)可以使用url添加標籤。

舉例:

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]); 

 結果:

2015-12-10 21:53:57.171 [4697:358837] Scheme: http2015-12-10 21:53:57.171 [4697:358837] Host: www.baidu.com2015-12-10 21:53:57.172 [4697:358837] Port: (null)2015-12-10 21:53:57.172 [4697:358837] Path: /s2015-12-10 21:53:57.172 [4697:358837] Relative path: /s2015-12-10 21:53:57.172 [4697:358837] Path components as array: ( "/",)2015-12-10 21:53:57.172 [4697:358837] Parameter string: (null)2015-12-10 21:53:57.173 [4697:358837] Query: tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=27092015-12-10 21:53:57.173 [4697:358837] Fragment: (null)2015-12-10 21:53:57.173 [4697:358837] User: (null)2015-12-10 21:53:57.173 [4697:358837] Password: (null)

ps:NSURL的用法

1:NSURL初始化方法:

NSURL *url=[NSURL URLWithString:@"http://www.baidu.com?id=1"]; 

2:解決NSURL初始化失敗的方法.

將傳進來的NSString 進行 UTF8 轉碼即可.

NSString *strLocalHtml = @"file:///Users/amarishuyi/Desktop/My IPhone Life/WebDeveloper/WebPlug-in/ExtEditor/DataPage/KMQT/Ext-HTMLEditor.html"; strLocalHtml = [NSString stringWithFormat:@"%@?Value=%@",strLocalHtml,self.txtUrl.text]; strLocalHtml= [strLocalHtml stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL * url=[NSURL URLWithString:strLocalHtml]; 

3:NSURL 成功初始化後可以擷取的參數 (摘自:NSURL 學習 )

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-03-31 18:22:20.904 SmallDemoList[5473:11603] 12131232 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Scheme: http 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Host: www.baidu.com 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Port: (null) 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Path: /s 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Relative path: /s 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Path components as array: (  "/", ) 2012-03-31 18:22:20.916 SmallDemoList[5473:11603] Parameter string: (null) 2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Query: tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709 2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Fragment: (null) 2012-03-31 18:22:20.917 SmallDemoList[5473:11603] User: (null) 2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Password: (null)

4:根據檔案名稱和檔案尾碼擷取程式包內容檔案的路徑

NSURL *urlKindEditor = [[NSBundlemainBundle] URLForResource:@"simple"withExtension:@"html"subdirectory:@"KindEditor/examples"]; 

URLForResource:檔案名稱
withExtension:檔案尾碼
subdirectory:在程式包中的哪個子目錄中尋找.

如果沒有找到將會返回nil
找到後返回如下路徑: file://localhost/Users/amarishuyi/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/FB0CDABC-D0E2-45FF-AA2C-959E8A65ADB4/SmallDemoList.app/KindEditor/examples/simple.html

以上內容是小編給大家分享的IOS開發中NSURL的基本操作及用法詳解,希望大家喜歡。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.