IOS開發之NSURL
來源:程序員人生 發布時間:2015-04-18 12:10:14 閱讀次數:3694次
1:NSURL初始化方法:
-
NSURL *url=[NSURL URLWithString:@"http://www.baidu.com?id=1"];
2:解決NSURL初始化失敗的相干解決方案.
將傳進來的NSString 進行 UTF8 轉碼便可.
1:針對 URLWithString 初始化失敗的解決方案
-
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:///"
3:NSURL 成功初始化后可以獲得的參數 (摘自:NSURL 學習 )
-
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]);
結果以下:
-
2012-03⑶1 18:22:20.904 SmallDemoList[5473:11603] 12131232
-
2012-03⑶1 18:22:20.907 SmallDemoList[5473:11603] Scheme: http
-
2012-03⑶1 18:22:20.907 SmallDemoList[5473:11603] Host: www.baidu.com
-
2012-03⑶1 18:22:20.907 SmallDemoList[5473:11603] Port: (null)
-
2012-03⑶1 18:22:20.907 SmallDemoList[5473:11603] Path: /s
-
2012-03⑶1 18:22:20.907 SmallDemoList[5473:11603] Relative path: /s
-
2012-03⑶1 18:22:20.907 SmallDemoList[5473:11603] Path components as array: (
-
"/",
-
s
-
)
-
2012-03⑶1 18:22:20.916 SmallDemoList[5473:11603] Parameter string: (null)
-
2012-03⑶1 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
-
2012-03⑶1 18:22:20.917 SmallDemoList[5473:11603] Fragment: (null)
-
2012-03⑶1 18:22:20.917 SmallDemoList[5473:11603] User: (null)
-
2012-03⑶1 18:22:20.917 SmallDemoList[5473:11603] Password: (null)
4:根據文件名稱和文件后綴獲得程序包內容文件的路徑
NSURL *urlKindEditor
= [[NSBundlemainBundle]URLForResource:@"simple"withExtension:@"html"subdirectory:@"KindEditor/examples"];
URLForResource:文件名稱
withExtension:文件后綴
subdirectory:在程序包中的哪一個子目錄中尋覓.
如果沒有找到將會返回nil
找到后返回以下路徑: file://localhost/Users/amarishuyi/Library/Application
Support/iPhone Simulator/5.1/Applications/FB0CDABC-D0E2⑷5FF-AA2C⑼59E8A65ADB4/SmallDemoList.app/KindEditor/examples/simple.html
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈