1.NSURL初始化方法:
方法一:
NSURL *url=[NSURL URLWithString:@"http://www.ubluesky.com?id=1"];方法二:
NSURL *baseURL = [NSURL URLWithString:@"file:///path/to/web_root/"]; NSURL *url = [NSURL URLWithString:@"folder/file.html" relativeToURL:baseURL]; NSURL *absURL = [url absoluteURL]; NSLog(@"absURL = %@", absURL);可以根据一个base URL地址和关联字符串来构造URL。
NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"]; [[NSURL URLWithString:@"foo" relativeToURL:baseURL] absoluteURL]; // http://example.com/v1/foo [NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL] absoluteURL]; // http://example.com/v1/foo?bar=baz [NSURL URLWithString:@"/foo" relativeToURL:baseURL] absoluteURL]; // http://example.com/foo [NSURL URLWithString:@"foo/" relativeToURL:baseURL] absoluteURL]; // http://example.com/v1/foo [NSURL URLWithString:@"/foo/" relativeToURL:baseURL] absoluteURL]; // http://example.com/foo/ [NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL] absoluteURL]; // http://example2.com/2:解决NSURL初始化失败的相关解决方案.
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];2>针对 fileURLWithPath 初始化失败的解决方案
self.filePathString = [self.filePathString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//此方法已废弃 NSURL *url = [NSURL fileURLWithPath:self.filePathString];转码成功后 会自动 在字符串左侧添加 “file:///”
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]);结果如下:
2016-05-04 16:51:44.425 URLSchemeDemo[19025:179974] Scheme: http 2016-05-04 16:51:44.426 URLSchemeDemo[19025:179974] Host: www.baidu.com 2016-05-04 16:51:44.426 URLSchemeDemo[19025:179974] Port: (null) 2016-05-04 16:51:44.426 URLSchemeDemo[19025:179974] Path: /s 2016-05-04 16:51:44.427 URLSchemeDemo[19025:179974] Relative path: /s 2016-05-04 16:51:44.427 URLSchemeDemo[19025:179974] Path components as array: ( "/", s ) 2016-05-04 16:51:44.427 URLSchemeDemo[19025:179974] Parameter string: (null) 2016-05-04 16:51:44.427 URLSchemeDemo[19025:179974] Query: tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709 2016-05-04 16:51:44.427 URLSchemeDemo[19025:179974] Fragment: (null) 2016-05-04 16:51:44.427 URLSchemeDemo[19025:179974] User: (null) 2016-05-04 16:51:44.428 URLSchemeDemo[19025:179974] Password: (null)4:根据文件名称和文件后缀获取程序包内容文件的路径
NSURL *urlKindEditor = [[NSBundle mainBundle] URLForResource:@"simple"withExtension:@"html" subdirectory:@"KindEditor/examples"];相关参数:
NSURL *url = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"mov"]];6.NSString 和 NSURL的互相转换
NSString *str = @""; NSURL *URL = [NSURL URLWithString:str]; //string->url NSString *str1 = [URL absoluteString]; //url->string