【问题标题】:Opening iTunes Store to specific song打开 iTunes Store 到特定歌曲
【发布时间】:2013-01-30 07:17:48
【问题描述】:

好的,我在这里看到了类似的问题,但没有一个真正为我回答问题。

我有一个流式音频应用程序,流源返回给我歌曲标题和艺术家姓名。我在应用程序中有一个 iTunes 按钮,并且想要打开 iTunes STORE(搜索)到那首确切的歌曲,或者至少关闭。我尝试了以下方法:


NSString *baseString = @"itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?songTerm=";

NSString *str1 = [self.songTitle2 stringByReplacingOccurrencesOfString:@" " withString:@"+"];

NSString *str2 = [self.artist2 stringByReplacingOccurrencesOfString:@" " withString:@"+"];

NSString *str = [NSString stringWithFormat:@"%@%@&artistTerm=%@", baseString, str1, str2];

[[UIApplication sharedApplication] openURL: [NSURL URLWithString:str]];

此调用确实按预期将我切换到 iTunes STORE,但随后弹出错误“无法连接到 iTunes Store”。我显然在线,因为这首歌正在积极播放,而且我在商店里。 iTunes 应用中的搜索框只显示歌曲名称,没有其他内容。

以下是生成字符串的示例: itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearchResults?artistTerm=Veruca+Salt&artistTerm=Volcano+Girls

我已经厌倦了将它生成的字符串粘贴到 Safari 中,它在我的 Mac 上运行良好,可以打开商店中艺术家的专辑。为什么不接电话?

此外,它似乎忽略了这两个项目,因为它没有带我去听那个艺术家的歌。这是否还需要知道专辑名称(我目前没有。)

我们将不胜感激。谢谢。

【问题讨论】:

  • 谢谢亚历克斯。我不知道该怎么做! :-)
  • 编辑标题以提及商店
  • 哦,我的错。我希望它打开 iTunes STORE,而不是 iTunes 本身。感谢您编辑 Daj-Djan。我也会编辑问题。
  • 您使用的示例没有使用 songTerm。而是使用了两次 ArtistTerm。

标签: iphone objective-c itunes-store


【解决方案1】:

是的,我正在回答我自己的问题。

经过大量挖掘并与我认识的最优秀的程序员之一交谈后,我们有了一个解决方案,所以我想我会在这里分享它。该解决方案采用歌曲名称和艺术家,实际上确实调用了 Link Maker API,取回一个 XML 文档,并提取必要的信息以创建指向 iTunes Store 的链接,通过以下方式打开专辑中歌曲的商店包含这首歌的艺术家。

在视图控制器的界面中,添加:

@property (strong, readonly, nonatomic) NSOperationQueue* operationQueue;
@property (nonatomic) BOOL searching;

在实现中:

@synthesize operationQueue = _operationQueue;
@synthesize searching = _searching;

以下是为您执行此操作的方法和代码:

// start an operation Queue if not started
-(NSOperationQueue*)operationQueue
{
    if(_operationQueue == nil) {
        _operationQueue = [NSOperationQueue new];
    }
    return _operationQueue;
}
// change searching state, and modify button and wait indicator (if you wish)
- (void)setSearching:(BOOL)searching
{
// this changes the view of the search button to a wait indicator while the search is     perfomed
// In this case
    _searching = searching;
    dispatch_async(dispatch_get_main_queue(), ^{
        if(searching) {
            self.searchButton.enabled = NO;
            [self.searchButton setTitle:@"" forState:UIControlStateNormal];
            [self.activityIndicator startAnimating];
        } else {
            self.searchButton.enabled = YES;
            [self.searchButton setTitle:@"Search" forState:UIControlStateNormal];
            [self.activityIndicator stopAnimating];
        }
    });
}
// based on info from the iTunes affiliates docs
// http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html
// this assume a search button to start the search. 
- (IBAction)searchButtonTapped:(id)sender {
    NSString* artistTerm = self.artistField.text;  //the artist text.
    NSString* songTerm = self.songField.text;      //the song text 
    // they both need to be non-zero for this to work right.
    if(artistTerm.length > 0 && songTerm.length > 0) {

        // this creates the base of the Link Maker url call.

        NSString* baseURLString = @"https://itunes.apple.com/search";
        NSString* searchTerm = [NSString stringWithFormat:@"%@ %@", artistTerm, songTerm];
        NSString* searchUrlString = [NSString stringWithFormat:@"%@?media=music&entity=song&term=%@&artistTerm=%@&songTerm=%@", baseURLString, searchTerm, artistTerm, songTerm];

        // must change spaces to +
        searchUrlString = [searchUrlString stringByReplacingOccurrencesOfString:@" " withString:@"+"];

        //make it a URL
        searchUrlString = [searchUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL* searchUrl = [NSURL URLWithString:searchUrlString];
        NSLog(@"searchUrl: %@", searchUrl);

        // start the Link Maker search
        NSURLRequest* request = [NSURLRequest requestWithURL:searchUrl];
        self.searching = YES;
        [NSURLConnection sendAsynchronousRequest:request queue:self.operationQueue completionHandler:^(NSURLResponse* response, NSData* data, NSError* error) {

            // we got an answer, now find the data.
            self.searching = NO;
            if(error != nil) {
                NSLog(@"Error: %@", error);
            } else {
                NSError* jsonError = nil;
                NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
                if(jsonError != nil) {
                    // do something with the error here
                    NSLog(@"JSON Error: %@", jsonError);
                } else {
                    NSArray* resultsArray = dict[@"results"];

                    // it is possible to get no results. Handle that here
                    if(resultsArray.count == 0) {
                        NSLog(@"No results returned.");
                    } else {

                        // extract the needed info to pass to the iTunes store search
                        NSDictionary* trackDict = resultsArray[0];
                        NSString* trackViewUrlString = trackDict[@"trackViewUrl"];
                        if(trackViewUrlString.length == 0) {
                            NSLog(@"No trackViewUrl");
                        } else {
                            NSURL* trackViewUrl = [NSURL URLWithString:trackViewUrlString];
                            NSLog(@"trackViewURL:%@", trackViewUrl);

                           // dispatch the call to switch to the iTunes store with the proper search url
                            dispatch_async(dispatch_get_main_queue(), ^{
                                [[UIApplication sharedApplication] openURL:trackViewUrl];
                            });
                        }
                    }
                }
            }
        }];
    }
}

返回的 XML 文件还有很多其他有用的信息,您也可以在这里提取,包括三种尺寸的专辑封面、专辑名称、成本等。

我希望这对其他人有所帮助。这让我困惑了很长时间,我感谢我的一个好朋友完成了这项工作。

【讨论】:

  • 一个快速说明:返回的数据是 JSON,而不是 XML。否则,感谢您提供信息!
  • 是的,我的错,我打字很快,XML 刚刚从手指中流出。你是对的,返回的是 JSON 数据。
【解决方案2】:

您实际上是在使用 URL 进行搜索。这就是 iTunes 在搜索时打开的原因。我在 Mac OS X 中的 iTunes 也会在搜索中打开。

使用 Search API for iTunes 搜索您想要的内容并获取艺术家、专辑或歌曲 ID,以便为该内容生成直接 URL。

查看 iTunes Link Maker 如何为艺术家或特定专辑创建 URL 并在您的应用中编写该 URL。

【讨论】:

  • 谢谢法比奥。我看到我的问题并不清楚。我实际上的意思是我希望 iTunes STORE 在搜索中打开(我解决了这个问题),所以这是我所期望的行为。我看过 iTunes Link Maker,我可以轻松地手动创建一个,但我看不到如何在歌曲名称和艺术家是动态的应用程序(使用 Objective-C)中使用 lik Maker,改变所有时间,所以我可以在我的代码中动态创建该链接。我也没有专辑名称或 ID,只有歌曲和艺术家。我仍然应该能够查看专辑列表。这就是困扰我的原因。
  • 我还要补充一点,它确实可以按预期打开搜索,但后来我得到那个商店无法连接错误,我仍然不明白。谢谢。
  • 好的。我得到了它。您无法从应用程序中使用链接生成器(也许如果您检查 POST 请求,您可以直接执行此操作),但它实际上不会生成搜索链接。
  • 为什么不使用搜索 API 并在您的应用程序中显示项目,然后在您拥有特定专辑或歌曲的 ID 时链接到商店?现在你在 iTunes Store 中显示搜索的要求是我认为的问题,因为 iOS iTunes Store 上没有高级搜索。
【解决方案3】:

现在 iOS 似乎已经在您尝试打开 iTunes html url 时直接打开了 iTunes 应用程序。

例如,尝试在 https://itunes.apple.com/br/album/falando-de-amor/id985523754 上执行 openURL 已打开 iTunes 应用程序而不是网站。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2013-03-30
    • 2013-08-27
    • 1970-01-01
    • 2014-02-25
    相关资源
    最近更新 更多