CFURLCreateStringByAddingPercentEscapes Quick Tip

It’s been a while since our last coding post for some reason our visitors like to view the photography section more, anyway i was working on a mac application today and found out that all URLs generated convert any # tag included in the url to %23. Please note that this is not an advanced tip, it’s just something for noobies that they might miss or don’t know how to do while still learning, I hope it helps everyone out there.

The conversion was caused by something called CFURLCreateStringByAddingPercentEscapes this usually replace certain characters with the equivalent percent escape sequence, so I wanted all that except converting the # tag my code was something like that

so if i type a URL like this http://example.com/page#1/ it comes out like this http://example.com/page%231/ which gives an error and doesn’t go anywhere, to fix this problem we had to remove the # tag from the legalURLCharactersToBeEscaped parameter and use CFSTR(“#”) for charactersToLeaveUnescaped, but before you do that you have to see the CFURL Reference

now you can see where the legalURLCharactersToBeEscaped and charactersToLeaveUnescaped are then replace them, so my code now will look like this

you can see that i removed the # inside the CFSTR and replaced the null with CFSTR(“#”).

To know more about the CFURL Reference please visit apple developer website