2014-10-11 69 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

相關問題