欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS中对Url进行编码和解码示例

程序员文章站 2023-12-20 16:16:16
本文主要介绍ios中对url进行编码和解码示例,具体如下: 1.非arc模式下 + (nsstring *)encodetopercentescapestr...

本文主要介绍ios中对url进行编码和解码示例,具体如下:

1.非arc模式下

+ (nsstring *)encodetopercentescapestring: (nsstring *) input {    // encode all the reserved characters, per rfc 3986    // ()

nsstring *outputstr = (nsstring *)cfurlcreatestringbyaddingpercentescapes(kcfallocatordefault,(cfstringref)input,null,(cfstringref)@"!*'();:@&=+$,/?%#[]",

kcfstringencodingutf8);

return outputstr;

}

+ (nsstring *)decodefrompercentescapestring: (nsstring *) input {

nsmutablestring *outputstr = [nsmutablestring stringwithstring:input];

[outputstr replaceoccurrencesofstring:@"+"

withstring:@" "

options:nsliteralsearch

range:nsmakerange(0, [outputstr length])];

return [outputstr stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];

2. arc模式下

- (nsstring *)encodetopercentescapestring: (nsstring *) input{  nsstring* 
outputstr = (__bridge nsstring *)cfurlcreatestringbyaddingpercentescapes(


                       null, /* allocator */

                       (__bridge cfstringref)input,

                       null, /* characterstoleaveunescaped */

                       (cfstringref)@"!*'();:@&=+$,/?%#[]",

                       kcfstringencodingutf8);
return
 outputstr;}- (nsstring *)decodefrompercentescapestring: (nsstring *) input
{nsmutablestring *outputstr = [nsmutablestring stringwithstring:input];[outputstr replaceoccurrencesofstring:@"+"withstring:@"
"options:nsliteralsearchrange:nsmakerange(0,
 [outputstr length])];return
 [outputstr stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: