What characters cause the escape sequence stringByAddingPercentEscapesUsingEncoding?

I had to switch from stringByAddingPercentEscapesUsingEncoding to CFURLCreateStringByAddingPercentEscapes because it does not avoid question marks (?). I'm curious what exactly this is happening and the rationale for partial acceleration versus RFC 3986.

+7
source share
2 answers

Some good categories are created to do what you need: http://iosdevelopertips.com/networking/a-better-url-encoding-method.html http://www.cocoanetics.com/2009/08/ url-encoding /

The rationale for leaving some characters outside of me ... except that the definition of the function is: Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.

To be completely correct, the + and and are legal characters in the URL, while the space is not. Consequently, the method will exit the space correctly, but leaves + and intact.

Reading RFC2396 http://www.ietf.org/rfc/rfc2396.txt - there is a set of reserved and non-reserved characters. I assume that none of these characters are escaped by stringByAddingPercentEscapesUsingEncoding.

+1
source

Be careful not to leak memory during conversions when using CFStringRef. This is what I came up with for working with Latin characters and others. I use this to avoid my options, not the entire URL. Depending on your use case, you may need to add or remove characters from "escapeChars"

 CFStringRef escapeChars = (CFStringRef)@"%;/?¿:@&=$+,[]#!'()*<>¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ \"\n"; NSString *encodedString = (__bridge_transfer NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge_retained CFStringRef) url, NULL, escapeChars, kCFStringEncodingUTF8); 

Hope this helps.

+5
source

All Articles