【问题标题】:Opening Apple Music from my iOS app从我的 iOS 应用程序打开 Apple Music
【发布时间】:2016-11-09 22:27:38
【问题描述】:

上下文:我目前正在开发一个与 Apple Music API 集成的 iOS 应用。我可以搜索歌曲并在我的应用程序上播放它们。为了尊重他们的政策,我必须允许用户在 Apple Music 应用上启动和播放歌曲。 Shazam 为 Apple Music、Deezer 和 Google Play 做到这一点(见下图)。

我已经使用this 线程为 Spotify 完成了该操作,基本上是使用 URL 方案。在 this Reddit 线程上,我找到了 iOS URL Scheme 的列表,但我找不到任何关于 Apple Music 的信息——同一线程上的this 请求确认它仍然不可用。

Apple Music API 也不行。

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

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

我是否遗漏了 API 文档中的某些内容?任何想法将不胜感激。

【问题讨论】:

  • 您指的是什么政策表明您需要允许用户在 Apple Music 中打开歌曲?

标签: ios objective-c iphone apple-music


【解决方案1】:

您应该使用深层链接在 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"]];

【讨论】:

  • 似乎是对的,会在这里试一试。谢谢!
【解决方案2】:

在这篇 SO 帖子中有一些示例代码,您可能会发现它们很有用: play apple music songs by third party applications。 我能够激活播放器并播放一首歌曲:

#import <MediaPlayer/MediaPlayer.h>

[[MPMusicPlayerController systemMusicPlayer] setQueueWithStoreIDs:tracks];
[[MPMusicPlayerController systemMusicPlayer] play];

另外,这里介绍了 Apple Music API 文档:

Apple Music Best Practices for App Developers

Apple Music API FAQ

希望对你有帮助。

【讨论】:

  • 这已经在应用程序中完成了,但这不是我想要的。我希望能够从我的应用启动 Apple Music 并在那里播放歌曲。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-13
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 2013-03-18
相关资源
最近更新 更多