iOS請(qǐng)求URL 中文轉(zhuǎn)譯

URL轉(zhuǎn)譯 stringByAddingPercentEscapesUsingEncoding(只對(duì) `#%^{}[]|"<> 加空格共14個(gè)字符編碼,不包括”&?”等符號(hào)), ios9將淘汰。
建議用stringByAddingPercentEncodingWithAllowedCharacters方法

//中文鏈接
NSString *globalURL = [[NSString alloc]initWithFormat:@"http://127.0.0.1:%d/oa/workflow/submit/workflow?requestId=181193&userNumber=01281209&type=submit&remark=這是個(gè)什么鬼啊,這是個(gè)神",ListenPort];
//iOS9.0廢棄方法
//NSString * encodingString = [globalURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//轉(zhuǎn)譯后的鏈接
NSString * encodingString = [globalURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

stringByAddingPercentEncodingWithAllowedCharacters需要傳一個(gè)

NSCharacterSet對(duì)象(關(guān)于 NSCharacterSet 這篇 文章說(shuō)的很好)

如[NSCharacterSet URLQueryAllowedCharacterSet]

@interface NSCharacterSet (NSURLUtilities)
// 預(yù)定義字符集用于六個(gè)URL組件和子組件,它們?cè)试S百分比編碼。 
- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters

// 包含URL用戶子組件中允許的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLUserAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

// 包含URL密碼子組件中允許的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLPasswordAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

// 包含URL的主機(jī)子組件中允許的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLHostAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

// 返回一個(gè)包含字符的字符集允許URL的路徑組件。字符“;”是一種合法的路徑,但是建議最好是percent-encoded兼容NSURL(-stringByAddingPercentEncodingWithAllowedCharacters:percent-encode任何‘;’字符如果你通過(guò)URLPathAllowedCharacterSet)
@property (class, readonly, copy) NSCharacterSet *URLPathAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

// 包含URL查詢(xún)組件中允許的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLQueryAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

// 包含URL片段組件中允許的字符的字符集
@property (class, readonly, copy) NSCharacterSet *URLFragmentAllowedCharacterSet API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

@end

編碼字符范圍
URLFragmentAllowedCharacterSet  "#%<>[\]^`{|}
URLHostAllowedCharacterSet      "#%/<>?@\^`{|}
URLPasswordAllowedCharacterSet  "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet      "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet     "#%<>[\]^`{|}
URLUserAllowedCharacterSet      "#%/:<>?@[\]^`

END

最后編輯于
?著作權(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),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 參考iOS開(kāi)發(fā)探索-Base64編碼iOS URL編碼&base64編碼URL安全的Base64編碼,解碼 為什么...
    differ_iOSER閱讀 7,982評(píng)論 5 17
  • 前言 在iOS程序中,訪問(wèn)一些HTTP/HTTPS的資源服務(wù)時(shí),如果url中存在中文或者特殊字符時(shí),會(huì)導(dǎo)致無(wú)法正常...
    wentianen閱讀 7,415評(píng)論 3 10
  • 優(yōu)先使用內(nèi)置的sorted函數(shù),原因: 算法最優(yōu) 內(nèi)置函數(shù)內(nèi)部是以C的速度運(yùn)行,速度更快 sorted常規(guī)是給列表...
    lupinwu閱讀 311評(píng)論 0 0
  • 0830(D45)親愛(ài)的金祿:感恩無(wú)非,讓你有機(jī)會(huì)和自己對(duì)話,讓浮躁的心靈得到片刻休息,讓贊美的語(yǔ)言充滿你的內(nèi)心,...
    胡金祿閱讀 216評(píng)論 0 0
  • 茫茫人海就像一片戈壁灘,我們就是灘中的沙礫,不過(guò)有你的做伴使我不再感到渺小和孤獨(dú)——笑瞇瞇的眼睛,胖乎乎的小臉,一...
    海深處閱讀 646評(píng)論 0 10

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