This article mainly introduces the iOS in the encoding and decoding of the URL examples, specifically as follows:
1. Non-arc mode
+ (NSString *) encodetopercentescapestring: (NSString *) input {//Encode all 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 mode
-(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];}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.