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。这个方法的行为由于其对子目录的/符号的处理而变得非常混乱无序。

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:[email protected]:443/script.ext;param=value?query=value#ref

NSUrl_第1张图片
屏幕快照 2016-07-05 上午10.15.20.png

BookMark

url的书签功能,即使文件移动,路径变化,也可以找到 。使用一个nsdata的封装。

  • 创建一个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;
    }

你可能感兴趣的:(NSUrl)