iOS技術(shù)文檔No.19 AppKit_NSURL

1.NSURL初始化方法:

NSURL *url=[NSURL URLWithString:@"http://www.baidu.com?id=1"];  

2.解決NSURL初始化失敗的相關(guān)解決方案.

將傳進(jìn)來的NSString 進(jìn)行 UTF8 轉(zhuǎn)碼即可.

1:針對(duì) 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:針對(duì) fileURLWithPath 初始化失敗的解決方案**
self.filePathString = [self.filePathString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
NSURL *url = [NSURL fileURLWithPath:self.filePathString];  

轉(zhuǎn)碼成功后 會(huì)自動(dòng) 在字符串左側(cè)添加 "file:///"

3:NSURL 成功初始化后可以獲取的參數(shù)
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]);  

打印:

2017-07-24 17:21:52.331 No.19 UIKit_NSURL[19557:1263699] Scheme: http
2017-07-24 17:21:52.331 No.19 UIKit_NSURL[19557:1263699] Host: www.baidu.com
2017-07-24 17:21:52.332 No.19 UIKit_NSURL[19557:1263699] Port: (null)
2017-07-24 17:21:52.332 No.19 UIKit_NSURL[19557:1263699] Path: /s
2017-07-24 17:21:52.333 No.19 UIKit_NSURL[19557:1263699] Relative path: /s
2017-07-24 17:21:52.334 No.19 UIKit_NSURL[19557:1263699] Path components as array: (
    "/",
    s
)
2017-07-24 17:21:52.334 No.19 UIKit_NSURL[19557:1263699] Parameter string: (null)
2017-07-24 17:21:52.334 No.19 UIKit_NSURL[19557:1263699] Query: tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709
2017-07-24 17:21:52.335 No.19 UIKit_NSURL[19557:1263699] Fragment: (null)
2017-07-24 17:21:52.335 No.19 UIKit_NSURL[19557:1263699] User: (null)
2017-07-24 17:21:52.335 No.19 UIKit_NSURL[19557:1263699] Password: (null)
4:根據(jù)文件名稱和文件后綴獲取程序包內(nèi)容文件的路徑
NSURL *urlKindEditor = [[NSBundlemainBundle]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
5:對(duì)比兩個(gè)URL 是否相等**
[url isEqual:[_audioPlayer url]]  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容