2014-10-11 54 views
0

我使用此代码,打开iTunes Store的应用程序和搜索特定的音乐:无法在iTunes Store中搜索iOS8上

NSString *iTunesLink = [NSString stringWithFormat:@"http://search.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?entity=album&media=all&page=1&restrict=true&startIndex=0&term=TERM_NAME"]; 
NSURL *url = [NSURL URLWithString:iTunesLink]; 
[[UIApplication sharedApplication] openURL:url]; 

代码工作正常上iOS7,通过改变TERM_NAME值我可以搜索任何我想。在iOS8上的问题是,不知何故搜索项附加和我使用的日志来检查什么是我的NSURL的值通过(“)符号的前缀,但它看起来不错。

enter image description here

+0

我有相同的问题......在这里你能解决这个 – Gooner 2014-10-23 14:47:31

+0

@Gooner - 我可以使用代码段下面列出,使这项工作 – 2014-10-24 13:56:39

回答

1

此代码工作了我:。

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]; 
        }); 
       } 
      } 
     } 
    } 
}]; 
+0

非常感谢美心,我有一个小问题,我现在正在看,但也许这是你知道的。从我的搜索返回的json结果都是为了美国itunes商店,但我实际上想要定位到爱尔兰的iTunes商店 – Gooner 2014-10-28 12:12:45

+0

明白了,简单的解决方法只需在搜索字符串中添加“country = country_code”即可定位到特定的国家/地区。再次感谢! – Gooner 2014-10-28 12:18:37

相关问题