Unable to search in iTunes Store on iOS8
Asked Answered
V

1

0

I'm using this code to open iTunes Store app and search for specific music:

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];

Code works fine on iOS7, by changing TERM_NAME value I can search whatever I want. The issue on iOS8 is that somehow search term is appended and prepended by ( " ) symbols. I'm using log to check what's the value of my NSURL but it looks fine.

enter image description here

Valerle answered 11/10, 2014 at 10:52 Comment(2)
I have the exact same problem...where you able to solve this?Cartilaginous
@Cartilaginous - I was able to make this work using code snippet listed below.Valerle
V
1

This code worked for me:

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];
                     });
                 }
             }
         }
     }
 }];
Valerle answered 24/10, 2014 at 13:55 Comment(2)
Thanks so much Maxim, I have one little issue, I am looking into right now, but maybe it is something you know about. The json results returned from my search are all for the US itunes store, but I actually want to target the Ireland iTunes storeCartilaginous
Got it, easy fix just add "country=country_code" to the search string if you want to target a specific country. Thanks again!Cartilaginous

© 2022 - 2024 — McMap. All rights reserved.