title: iOS open iTunes music search with given keywords from app
body: <p>I have build an app that displays videos with background music and users can add the meta-data (artis, title, album, genre) of the song to be displayed next to the video. </p>
  
  <p>Now I want to enable the viewers to buy this song in iTunes.</p>
  
  <p>I query the iTunes search (<a href="https://itunes.apple.com/search" rel="nofollow">https://itunes.apple.com/search</a>) to get a link to open the specific song, which works nice 95% of the time. I validate the result by comparing the returned artist and songtitle to make sure I don't point the user to a "false positive" song. </p>
  
  <p>In case I cant find a perfect match I would like to open iTunes and pre-fill the title and artist in the song field. And I am having a hard time to get it working.</p>
  
  <p>My best effort so far was to open itms://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search and add ?term=My+keywords&amp;media=music&amp;entity=song which opens iTunes with the search view presented and has already filled in "My keywords", but unfortunately the search term is messed up and displayed as <em>( "My keywords" )</em> including the quotation marks and brackets.</p>
  
  <p>This is the code I am using: </p>
  
  <pre><code>NSString* baseURLString2 = @"itms://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search";
  NSString* searchUrlString2 = [NSString stringWithFormat:@"%@?term=%@&amp;media=music&amp;entity=song", baseURLString2, searchTerm];
  
  searchUrlString2 = [searchUrlString2 stringByReplacingOccurrencesOfString:@" " withString:@"+"];
  searchUrlString2 = [searchUrlString2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  
  NSURL* searchUrl2 = [NSURL URLWithString:searchUrlString2];
  
  dispatch_async(dispatch_get_main_queue(), ^{
      [[UIApplication sharedApplication] openURL:searchUrl2];
  });
  </code></pre>
  
  <p>This is an example for a searchUrlString2: </p>
  
  <pre><code>itms://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?term=J+Cole+Trouble&amp;media=music&amp;entity=song
  </code></pre>
  
  <p>When I use https instead of itms it works fine in a browser on my MacBook, but still the same in iTunes on iOS 8.</p>
  
  <p>Any ideas on how to fix this?
  Or can you point me to a good documentation for this?</p>
  
  <p>Thanks in advance
  SVen</p>