从我的iOS应用程序中打开Apple Music

15
背景:我目前正在开发一个与 Apple Music API 集成的 iOS 应用程序。我可以在我的应用程序中搜索歌曲并播放它们。为了尊重他们的政策,我必须允许用户启动和在 Apple Music 应用程序中播放歌曲。Shazam 对于 Apple Music、Deezer 和 Google Play 均如此(请参见下图)。

enter image description here

我已经使用线程来实现了在 Spotify 上这样做,基本上是使用 URL 方案。在 Reddit 线程上,我找到了一份 iOS URL Scheme 列表,但我找不到任何关于 Apple Music 的信息 – 同一线程上的此请求确认它仍然不可用。

Apple Music API 也没有运气。

问题:有人知道如何在 Apple Music 上实现相同的行为吗?顺便说一下,它不需要使用 URL 方案。

这里的目标是启动 Apple Music 并将歌曲播放到 Apple Music 应用程序中,而不是在我的应用程序中。我已经在我的应用程序上播放了来自 Apple Music 的歌曲。

我是否在 API 文档上遗漏了什么?任何想法都将不胜感激。


你所指的政策是什么,要求你必须允许用户在Apple Music中打开一首歌曲? - Jordi Bruin
2个回答

10

为了在Apple Music应用程序中打开曲目,您应该使用深层链接:

https://affiliate.itunes.apple.com/resources/documentation/linking-to-the-itunes-music-store/

首先,您需要通过SKCloudServiceController API请求授权以检查您的功能(例如,如果您的设备允许播放Apple Music曲目)。

[SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
        self.cloudServiceController = [[SKCloudServiceController alloc] init];
        [self.cloudServiceController requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities, NSError * _Nullable error) {           
            [self.cloudServiceController requestStorefrontIdentifierWithCompletionHandler:^(NSString * _Nullable storefrontIdentifier,
                                                                                            NSError * _Nullable error) {
                NSString *identifier = [[storefrontIdentifier componentsSeparatedByString:@","] firstObject];
                identifier = [[identifier componentsSeparatedByString:@"-"] firstObject];
                NSString *countryCode = [self countryCodeWithIdentifier:identifier];                  
            }];

        }];
    }];

接下来,您将能够请求商店前端标识符,您将使用它来定义您的国家/地区代码。我建议在您的项目中包含一个.plist文件,其中包含所有标识符和相应的国家/地区代码。(您可以在此处找到.plist文件https://github.com/bendodson/storefront-assistant/blob/master/StorefrontCountries.plist)。您需要使用您的国家/地区代码进行Apple Music API请求。

- (NSString *)countryCodeWithIdentifier:(NSString *)identifier {
     NSURL *plistURL = [[NSBundle mainBundle] URLForResource:@"CountryCodes" withExtension:@"plist"];
     NSDictionary *countryCodeDictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];

     return countryCodeDictionary[identifier];
}

一旦获得了相应的国家代码,您就可以在Apple Music的API中搜索曲目。使用以下参数向GET https://itunes.apple.com/search发出请求:

 NSDictionary *parameters = @{
                               @"isStreamable" : @(YES),
                               @"term" : @"your search parameter"
                               @"media" : @"music",
                               @"limit" : @(5),
                               @"country" : @"your country code"
                               };

作为对此请求的回应,您将收到一系列与许多参数相关联的音轨结果数组。其中一个是 "trackViewUrl"。只需将以下参数添加到该 "trackViewUrl" 中,即可使其成为 Apple Music 应用程序的深度链接:

NSString *appleMusicDeepLinking = [NSString stringWithFormat:@"%@&mt=1&app=music", response[0][@"trackViewUrl"]];

看起来没问题,我会在这里试一下。谢谢! - antonioduarte

0

这已经在应用程序中实现了,但不是我想要的。我希望能够从我的应用程序启动Apple Music并在那里播放歌曲。 - antonioduarte

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接