此代碼工作了我:。
NSString *artist = @"artist";
NSString *title = @"title";
NSOperationQueue *operationQueue = [NSOperationQueue new];
NSString *baseURLString = @"https://itunes.apple.com/search";
NSString *searchTerm = [NSString stringWithFormat:@"%@ %@", artist, title];
NSString *searchUrlString = [NSString stringWithFormat:@"%@?media=music&entity=song&term=%@&artistTerm=%@&songTerm=%@", baseURLString, searchTerm, artist, title];
searchUrlString = [searchUrlString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
searchUrlString = [searchUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *searchUrl = [NSURL URLWithString:searchUrlString];
NSURLRequest *request = [NSURLRequest requestWithURL:searchUrl];
[NSURLConnection sendAsynchronousRequest:request queue:operationQueue completionHandler:^(NSURLResponse* response, NSData* data, NSError* error)
{
if (error)
{
NSLog(@"Error: %@", error);
}
else
{
NSError *jsonError = nil;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (jsonError)
{
NSLog(@"JSON Error: %@", jsonError);
}
else
{
NSArray *resultsArray = dict[@"results"];
if(resultsArray.count == 0)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"917xfm" message:[NSString stringWithFormat:@"No results returned."] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
});
}
else
{
NSDictionary *trackDict = resultsArray[0];
NSString *trackViewUrlString = trackDict[@"trackViewUrl"];
if (trackViewUrlString.length)
{
NSURL *trackViewUrl = [NSURL URLWithString:trackViewUrlString];
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] openURL:trackViewUrl];
});
}
}
}
}
}];
我有相同的問題......在這裏你能解決這個 – Gooner 2014-10-23 14:47:31
@Gooner - 我可以使用代碼段下面列出,使這項工作 – 2014-10-24 13:56:39