问题
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.
Now I want to enable the viewers to buy this song in iTunes.
I query the iTunes search (https://itunes.apple.com/search) 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.
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.
My best effort so far was to open itms://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search and add ?term=My+keywords&media=music&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 ( "My keywords" ) including the quotation marks and brackets.
This is the code I am using:
NSString* baseURLString2 = @"itms://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search";
NSString* searchUrlString2 = [NSString stringWithFormat:@"%@?term=%@&media=music&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];
});
This is an example for a searchUrlString2:
itms://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?term=J+Cole+Trouble&media=music&entity=song
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.
Any ideas on how to fix this? Or can you point me to a good documentation for this?
Thanks in advance SVen
回答1:
Found it, using itms://itunes.apple.com/WebObjects/MZStore.woa/wa/search as baseURL did the trick for me.
So the complete URL is:
itms://itunes.apple.com/WebObjects/MZStore.woa/wa/search?term=MySearch&media=music&entity=song
来源:https://stackoverflow.com/questions/26027416/ios-open-itunes-music-search-with-given-keywords-from-app