是的,我正在回答我自己的问题。
在与我认识的最好的程序员之一进行大量挖掘和交谈之后,我们有了一个解决方案,所以我想我会在这里分享它。该解决方案采用歌曲名称和艺术家,实际上确实调用了 Link Maker API,取回一个 XML 文档,并提取必要的信息以创建指向 iTunes Store 的链接,通过以下方式打开专辑中歌曲的商店包含这首歌的艺术家。
在视图控制器的界面中,添加:
@property (strong, readonly, nonatomic) NSOperationQueue* operationQueue;
@property (nonatomic) BOOL searching;
在实施中:
@synthesize operationQueue = _operationQueue;
@synthesize searching = _searching;
以下是将为您执行此操作的方法和代码:
// start an operation Queue if not started
-(NSOperationQueue*)operationQueue
{
if(_operationQueue == nil) {
_operationQueue = [NSOperationQueue new];
}
return _operationQueue;
}
// change searching state, and modify button and wait indicator (if you wish)
- (void)setSearching:(BOOL)searching
{
// this changes the view of the search button to a wait indicator while the search is perfomed
// In this case
_searching = searching;
dispatch_async(dispatch_get_main_queue(), ^{
if(searching) {
self.searchButton.enabled = NO;
[self.searchButton setTitle:@"" forState:UIControlStateNormal];
[self.activityIndicator startAnimating];
} else {
self.searchButton.enabled = YES;
[self.searchButton setTitle:@"Search" forState:UIControlStateNormal];
[self.activityIndicator stopAnimating];
}
});
}
// based on info from the iTunes affiliates docs
// http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html
// this assume a search button to start the search.
- (IBAction)searchButtonTapped:(id)sender {
NSString* artistTerm = self.artistField.text; //the artist text.
NSString* songTerm = self.songField.text; //the song text
// they both need to be non-zero for this to work right.
if(artistTerm.length > 0 && songTerm.length > 0) {
// this creates the base of the Link Maker url call.
NSString* baseURLString = @"https://itunes.apple.com/search";
NSString* searchTerm = [NSString stringWithFormat:@"%@ %@", artistTerm, songTerm];
NSString* searchUrlString = [NSString stringWithFormat:@"%@?media=music&entity=song&term=%@&artistTerm=%@&songTerm=%@", baseURLString, searchTerm, artistTerm, songTerm];
// must change spaces to +
searchUrlString = [searchUrlString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
//make it a URL
searchUrlString = [searchUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* searchUrl = [NSURL URLWithString:searchUrlString];
NSLog(@"searchUrl: %@", searchUrl);
// start the Link Maker search
NSURLRequest* request = [NSURLRequest requestWithURL:searchUrl];
self.searching = YES;
[NSURLConnection sendAsynchronousRequest:request queue:self.operationQueue completionHandler:^(NSURLResponse* response, NSData* data, NSError* error) {
// we got an answer, now find the data.
self.searching = NO;
if(error != nil) {
NSLog(@"Error: %@", error);
} else {
NSError* jsonError = nil;
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if(jsonError != nil) {
// do something with the error here
NSLog(@"JSON Error: %@", jsonError);
} else {
NSArray* resultsArray = dict[@"results"];
// it is possible to get no results. Handle that here
if(resultsArray.count == 0) {
NSLog(@"No results returned.");
} else {
// extract the needed info to pass to the iTunes store search
NSDictionary* trackDict = resultsArray[0];
NSString* trackViewUrlString = trackDict[@"trackViewUrl"];
if(trackViewUrlString.length == 0) {
NSLog(@"No trackViewUrl");
} else {
NSURL* trackViewUrl = [NSURL URLWithString:trackViewUrlString];
NSLog(@"trackViewURL:%@", trackViewUrl);
// dispatch the call to switch to the iTunes store with the proper search url
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] openURL:trackViewUrl];
});
}
}
}
}
}];
}
}
返回的 XML 文件还有很多其他有用的信息,您也可以在这里提取,包括三种尺寸的专辑封面、专辑名称、成本等。
我希望这对其他人有所帮助。这让我困惑了很长一段时间,我感谢我的一个好朋友完成了这项工作。