IOS開發之NSURL

來源:互聯網
上載者:User

IOS開發之NSURL

1:NSURL初始化方法:


  1. NSURL *url=[NSURL URLWithString:@www.bkjia.comid=1];

     

    2:解決NSURL初始化失敗的相關解決方案.

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

    1:針對 URLWithString 初始化失敗的解決方案

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

      2:針對 fileURLWithPath 初始化失敗的解決方案

      1. self.filePathString = [self.filePathString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
      2. NSURL *url = [NSURL fileURLWithPath:self.filePathString];
        轉碼成功後 會自動 在字串左側添加 file:///


         

         

         

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

         

        1. NSURL *url = [NSURL URLWithString: @www.bkjia.com?tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709];
        2. NSLog(@Scheme: %@, [url scheme]);
        3. NSLog(@Host: %@, [url host]);
        4. NSLog(@Port: %@, [url port]);
        5. NSLog(@Path: %@, [url path]);
        6. NSLog(@Relative path: %@, [url relativePath]);
        7. NSLog(@Path components as array: %@, [url pathComponents]);
        8. NSLog(@Parameter string: %@, [url parameterString]);
        9. NSLog(@Query: %@, [url query]);
        10. NSLog(@Fragment: %@, [url fragment]);
        11. NSLog(@User: %@, [url user]);
        12. NSLog(@Password: %@, [url password]);

          結果如下:

          1. 2012-03-31 18:22:20.904 SmallDemoList[5473:11603] 12131232
          2. 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Scheme: http
          3. 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Host: www.baidu.com
          4. 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Port: (null)
          5. 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Path: /s
          6. 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Relative path: /s
          7. 2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Path components as array: (
          8. /,
          9. s
          10. )
          11. 2012-03-31 18:22:20.916 SmallDemoList[5473:11603] Parameter string: (null)
          12. 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
          13. 2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Fragment: (null)
          14. 2012-03-31 18:22:20.917 SmallDemoList[5473:11603] User: (null)
          15. 2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Password: (null)

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

            NSURL *urlKindEditor = [[NSBundlemainBundle]URLForResource:@simplewithExtension:@htmlsubdirectory:@KindEditor/examples];

             

            URLForResource:檔案名稱

            withExtension:檔案尾碼

            subdirectory:在程式包中的哪個子目錄中尋找.

            如果沒有找到將會返回nil

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.