NSUrl
初始化 :NSURL *url=[NSURL URLWithString:@"http://www.baidu.com?id=1"];
NSURL *url = [NSURL fileURLWithPath:self.filePathString];
當(dāng)初始化失敗的時(shí)候伞广,需要考慮的是編碼問題 裙顽。把字符串進(jìn)行utf-8編碼 。
strLocalHtml= [strLocalHtml stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
根據(jù)文件名稱和文件后綴獲取程序包內(nèi)容文件的路徑
NSURL *urlKindEditor = [[NSBundle mainBundle]URLForResource:@"simple"withExtension:@"html"subdirectory:@"KindEditor/examples"];
- URLForResource:文件名稱
- withExtension:文件后綴
- subdirectory:在程序包中的哪個(gè)子目錄中尋找.
如果沒有找到將會(huì)返回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:
可以根據(jù)一個(gè) base URL 地址和關(guān)聯(lián)字符串來構(gòu)造 URL徘层。這個(gè)方法的行為由于其對(duì)子目錄的/符號(hào)的處理而變得非常混亂無序畜伐。
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各個(gè)部分的含義
https://johnny:p4ssw0rd@www.example.com:443/script.ext;param=value?query=value#ref
屏幕快照 2016-07-05 上午10.15.20.png
BookMark
url的書簽功能忘分,即使文件移動(dòng)策添,路徑變化,也可以找到 东亦。使用一個(gè)nsdata的封裝杏节。
- 創(chuàng)建一個(gè)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;
}