iOS open iTunes music search with given keywords from app

你说的曾经没有我的故事 提交于 2019-12-13 19:13:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!