title: Cannot load a NSURL with spaces or hashtags in iPhone app
body: <p>Twitter has a nice feature that allows you to preload status messages using the following format:</p>
  
  <pre><code>http://twitter.com/?status=@HelloWorld Hello World
  </code></pre>
  
  <p>or alternatively:</p>
  
  <pre><code>http://twitter.com/?status=%40HelloWorld%20Hello%20World
  </code></pre>
  
  <p>I am trying to add a button to my iPhone app which will open Safari to the above pre-populated tweet.</p>
  
  <p>However I am running into the problem where the percentage signs are being double escaped.</p>
  
  <p>Here is the code that I have tried:</p>
  
  <h2>First an example of what does work</h2>
  
  <pre><code>NSString* urlText = @"http://www.twitter.com/home?status=@HelloWorld";
  NSURL *url = [NSURL URLWithString: urlText];
  if (![[UIApplication sharedApplication] openURL:(NSURL*)url])
          NSLog(@"%@%@",@"Failed to open url:",[url description]);
  </code></pre>
  
  <p>This code works like a charm and outputs:</p>
  
  <pre><code>http://twitter.com/?status=%40HelloWorld
  </code></pre>
  
  <h2>Code that doesn't work</h2>
  
  <pre><code>NSString* urlText = @"http://www.twitter.com/home?status=@HelloWorld Hello World";
  NSURL *url = [NSURL URLWithString: urlText];
  if (![[UIApplication sharedApplication] openURL:(NSURL*)url])
          NSLog(@"%@%@",@"Failed to open url:",[url description]);
  </code></pre>
  
  <p>This creates a null NSURL. This I can only assume because URLWithString does not accept text with spaces in it.</p>
  
  <p>So I tried this code:</p>
  
  <pre><code>NSString* urlText = @"http://www.twitter.com/home?status=@HelloWorld%20Hello%20World";
  NSURL *url = [NSURL URLWithString: urlText];
  if (![[UIApplication sharedApplication] openURL:(NSURL*)url])
     NSLog(@"%@%@",@"Failed to open url:",[url description]);
  </code></pre>
  
  <p>However that creates the URL:</p>
  
  <pre><code>http://twitter.com/?status=%40HelloWorld%2520Hello%2520World
  </code></pre>
  
  <p>So I have escaped my percentage sign %, which is of course not what I intended.</p>
  
  <p>Of course people have been talking about using the function: stringByAddingPercentEscapesUsingEncoding</p>
  
  <p>So I wrote this code:</p>
  
  <pre><code>NSString* urlText = @"http://www.twitter.com/home?status=@HelloWorld%20Hello%20World";
  urlText = [urlText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  
  NSURL *url = [NSURL URLWithString: urlText];
  if (![[UIApplication sharedApplication] openURL:(NSURL*)url])
      NSLog(@"%@%@",@"Failed to open url:",[url description]);
  </code></pre>
  
  <p>However you again get the double escaping problem:</p>
  
  <pre><code>http://twitter.com/?status=%40HelloWorld%2520Hello%2520World
  </code></pre>
  
  <p>I'm hoping that someone might know some sort of work around. Ideally I would like to also include hashtags, but for now just getting spaces in would be a huge step forward.</p>