溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

NSURL

發(fā)布時(shí)間:2020-06-22 01:31:46 來(lái)源:網(wǎng)絡(luò) 閱讀:453 作者:15010878114 欄目:開發(fā)技術(shù)

1:NSURL初始化方法:

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

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

將傳進(jìn)來(lái)的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]);  

結(jié)果如下:

2012-03-31 18:22:20.904 SmallDemoList[5473:11603] 12131232  2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Scheme: http  2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Host: www.baidu.com  2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Port: (null2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Path: /s  2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Relative path: /s  2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Path components as array: ("/",2012-03-31 18:22:20.916 SmallDemoList[5473:11603] Parameter string: (null2012-03-31 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-31 18:22:20.917 SmallDemoList[5473:11603] Fragment: (null2012-03-31 18:22:20.917 SmallDemoList[5473:11603] User: (null2012-03-31 18:22:20.917 SmallDemoList[5473:11603] 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]] 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI