2016-11-09 39 views
13

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

enter image description here

我做到了这一点,使用Spotify的this线,基本上使用的URL方案。在this Reddit线程我找到了iOS URL Scheme的列表,但我找不到有关Apple Music的任何内容 - this请求在同一个线程上确认它仍然不可用。

对Apple Music API也没有好运气。

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

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

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

+0

什么样的政策,你指的是说,你需要允许用户打开苹果音乐的歌曲? –

回答

7

您应该使用深层链接,以便在苹果音乐应用打开图钉:,你需要申请通过SKCloudServiceController API的授权,检查你的能力( https://affiliate.itunes.apple.com/resources/documentation/linking-to-the-itunes-music-store/

,首先例如,如果你的设备允许的回放Apple音乐曲目)。

[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 /搜索

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

作为该请求的响应,会收到轨道结果的阵列,有很多的相关联的参数。其中之一是“trackViewUrl”。只需以下参数添加到该trackViewUrl以使其深度链接到苹果的音乐应用程序:

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

似乎是正确的,会在这里试一试。谢谢! – antonioduarte

+0

为我工作 - 谢谢! –

0

在SO贴子中有一些示例代码,您可能会觉得有用: play apple music songs by third party applications。 我能够激活播放器和播放一首歌曲有:

#import <MediaPlayer/MediaPlayer.h> 

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

此外,苹果音乐API文档在这里介绍:

Apple Music Best Practices for App Developers

Apple Music API FAQ

我希望它能帮助。

+0

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

相关问题