NSUrl

NSUrl

初始化 :NSURL *url=[NSURL URLWithString:@"http://www.baidu.com?id=1"];
NSURL *url = [NSURL fileURLWithPath:self.filePathString];

當初始化失敗的時候,需要考慮的是編碼問題 。把字符串進行utf-8編碼 。
strLocalHtml= [strLocalHtml stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

根據文件名稱和文件后綴獲取程序包內容文件的路徑

NSURL *urlKindEditor = [[NSBundle mainBundle]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

+URLWithString:relativeToURL:

可以根據一個 base URL 地址和關聯字符串來構造 URL。這個方法的行為由于其對子目錄的/符號的處理而變得非?;靵y無序。

NSURL *baseURL = [NSURL fileURLWithString:@"file:///path/to/user/"];
NSURL *URL = [NSURL URLWithString:@"folder/file.html" relativeToURL:baseURL];
NSLog(@"absoluteURL = %@", [URL absoluteURL]);
NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];

[NSURL URLWithString:@"foo" relativeToURL:baseURL];
// http://example.com/v1/foo

[NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL];
// http://example.com/v1/foo?bar=baz

[NSURL URLWithString:@"/foo" relativeToURL:baseURL];
// http://example.com/foo

[NSURL URLWithString:@"foo/" relativeToURL:baseURL];
// http://example.com/v1/foo

[NSURL URLWithString:@"/foo/" relativeToURL:baseURL];
// http://example.com/foo/

[NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL];
// http://example2.com/

url各個部分的含義

https://johnny:p4ssw0rd@www.example.com:443/script.ext;param=value?query=value#ref

屏幕快照 2016-07-05 上午10.15.20.png

BookMark

url的書簽功能,即使文件移動,路徑變化,也可以找到 。使用一個nsdata的封裝。

  • 創(chuàng)建一個BookMark

/- (NSData*)bookmarkForURL:(NSURL*)url {

    NSError* theError = nil;        
    NSData* bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationSuitableForBookmarkFile
                                            includingResourceValuesForKeys:nil
                                            relativeToURL:nil
                                            error:&theError];
    if (theError || (bookmark == nil)) {
        // Handle any errors.
        return nil;
    }
    return bookmark;
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容