2016-11-09 70 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

相關問題