title: Need to encode NSString for URL. CFURLCreateStringByAddingPercentEscapes mangling my strings
body: <p>My music player app needs to be able to send song, artist, and album names to Last.fm via the REST API. In order to properly POST these values I need to encode them so they are URL safe (replace &amp;, /, +, etc.)</p>
  
  <p>I've done a lot of research but I cannot find an answer that works for me. The most common answer I found on the web and here on StackOverflow was this:</p>
  
  <pre><code>NSString * encodedString = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(__bridge CFStringRef)stringToEncode,NULL,(CFStringRef)@"!*'();:@&amp;=+$,/?%#[]",kCFStringEncodingUTF8 );
  </code></pre>
  
  <p>However, the results of this don't seem to be what I should expect. Example from NSLog showing the before and after results:</p>
  
  <p><strong>Artist</strong>:</p>
  
  <pre><code>2012-09-11 10:20:27.803 albums[7647:5c03] Depeche Mode
  2012-09-11 10:20:27.804 albums[7647:5c03] Depeche2ode
  </code></pre>
  
  <p><strong>Song</strong>:</p>
  
  <pre><code>2012-09-11 10:20:27.807 albums[7647:5c03] I Want It All (Album Version)
  2012-09-11 10:20:27.808 albums[7647:5c03] I2ant2t-0X1.1A1A10D39EP-485ll228Album2ersion2
  </code></pre>
  
  <p><strong>Album</strong>:</p>
  
  <pre><code>2012-09-11 10:20:27.810 albums[7647:5c03] Playing The Angel
  2012-09-11 10:20:27.811 albums[7647:5c03] Playing2he-0X1.1A1A10D39EP-485ngel
  </code></pre>
  
  <p>Rather than just replacing the reserved chars with escape codes it is mangling my strings.</p>
  
  <p>I've tried the NSString method of doing this and I get the same thing:</p>
  
  <pre><code>NSString *encodedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  </code></pre>
  
  <p><strong>Artist</strong>:</p>
  
  <pre><code>2012-09-11 10:29:53.780 albums[7675:5503] Depeche Mode
  2012-09-11 10:29:53.787 albums[7675:5503] Depeche2ode
  </code></pre>
  
  <p><strong>Song</strong>:</p>
  
  <pre><code>2012-09-11 10:29:53.789 albums[7675:5503] I Want It All (Album Version)
  2012-09-11 10:29:53.790 albums[7675:5503] I2ant2t-0X1.1A1A100A78P-485ll2Album2ersion)
  </code></pre>
  
  <p><strong>Album</strong>:</p>
  
  <pre><code>2012-09-11 10:29:53.792 albums[7675:5503] Playing The Angel
  2012-09-11 10:29:53.793 albums[7675:5503] Playing2he-0X1.1A1A100A78P-485ngel
  </code></pre>
  
  <p>I've searched far and wide and can find no occurrence of exactly the same problem. The solution listed above (which is suggested on dozens of blogs and SO posts) seems to work for other folks.</p>
  
  <p>I don't really understand string encoding. I've never dealt with it on another platform so I can't tell you how the string I am trying substitute with escape codes is encoded. I can tell you that I am getting it from the MPMediaItem method <code>valueForKey:MPMediaItemPropertyTitle</code>, so whatever it is it is what Apple is returning. The strings returned from that look find when NSLogged or in the UI.</p>