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

ant*_*rte 14 iphone objective-c ios apple-music

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

在此输入图像描述

我已经使用这个线程为Spotify做了这个,基本上使用了URL方案.在这个 Reddit线程上,我找到了一个iOS URL Scheme的列表,但我找不到任何关于Apple Music的信息 - 这个请求在同一个线程上确认它仍然无法使用.

Apple Music API也没有运气.

问题:有人知道如何使用Apple Music实现相同的行为吗?顺便说一下,没有必要使用URL方案.

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

我错过了API文档的内容吗?任何想法将不胜感激.

Fla*_*urt 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];                  
            }];

        }];
    }];
Run Code Online (Sandbox Code Playgroud)

接下来,您将能够请求商店前端标识符,您将使用该标识符来定义国家/地区代码.我建议在项目中包含一个.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];
}
Run Code Online (Sandbox Code Playgroud)

获得相应的国家/地区代码后,您就可以在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"
                               };
Run Code Online (Sandbox Code Playgroud)

作为此请求的响应,您将收到一系列跟踪结果,其中包含许多参数.其中之一是"trackViewUrl".只需将以下参数添加到此trackViewUrl,以便深入链接到Apple Music应用程序:

NSString *appleMusicDeepLinking = [NSString stringWithFormat:@"%@&mt=1&app=music", response[0][@"trackViewUrl"]];
Run Code Online (Sandbox Code Playgroud)