gpt4 book ai didi

ios - 无法在 iOS8 上的 iTunes Store 中搜索

转载 作者:行者123 更新时间:2023-11-29 02:36:27 25 4
gpt4 key购买 nike

我正在使用此代码打开 iTunes Store 应用程序并搜索特定音乐:

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 上的问题是,搜索词以某种方式附加并以 ( ") 符号开头。我使用 log 来检查 NSURL 的值,但它看起来不错。

enter image description here

最佳答案

这段代码对我有用:

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

关于ios - 无法在 iOS8 上的 iTunes Store 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26313839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com