Reputation: 655
Context: I'm currently developing an iOS app that integrates with Apple Music API. I'm able to search for songs and play them on my app. To respect their policies I've to allow the user to launch and play the song on Apple Music app. Shazam does that for Apple Music, Deezer and Google Play (see image below).
I've done that for Spotify using this thread, basically using a URL scheme. On this Reddit thread I found a list of iOS URL Scheme's, but I can't find anything about Apple Music – this request on the same thread confirms that it's still not available.
No luck with the Apple Music API as well.
Question: Does someone knows how to accomplish the same behavior with Apple Music? Btw, it doesn't necessary needs to be with URL schemes.
The goal here is to launch the Apple Music and play the song into the Apple Music app, not on my app. I'm already playing songs from Apple Music on my app.
Am I missing something on the API docs? Any thoughts will be appreciated.
Upvotes: 15
Views: 4415
Reputation: 332
You should use deeplinks in order to open tacks in Apple Music app: https://affiliate.itunes.apple.com/resources/documentation/linking-to-the-itunes-music-store/
First of all, you need to request an authorization via SKCloudServiceController API to check your capabilities (e.g., if your device allows playback of Apple Music tracks).
[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];
}];
}];
}];
Next, you'll be able to request the store front identifier, which you are going to use to define your country code. I suggest to include a .plist file in your project with all the identifiers and respective country codes. (you can find the .plist file here https://github.com/bendodson/storefront-assistant/blob/master/StorefrontCountries.plist). You need your country code to the Apple Music API requests.
- (NSString *)countryCodeWithIdentifier:(NSString *)identifier {
NSURL *plistURL = [[NSBundle mainBundle] URLForResource:@"CountryCodes" withExtension:@"plist"];
NSDictionary *countryCodeDictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];
return countryCodeDictionary[identifier];
}
Once you have the respective country code, you'll be able to search for a track in the Apple Music's API. Make a request to GET https: //itunes.apple.com/search using the following parameters:
NSDictionary *parameters = @{
@"isStreamable" : @(YES),
@"term" : @"your search parameter"
@"media" : @"music",
@"limit" : @(5),
@"country" : @"your country code"
};
As a response of this request, you'll receive an array of track results, with lots of parameters associated. One of them is the "trackViewUrl". Just add the following parameters to this trackViewUrl in order to make it deep linking to Apple Music app:
NSString *appleMusicDeepLinking = [NSString stringWithFormat:@"%@&mt=1&app=music", response[0][@"trackViewUrl"]];
Upvotes: 10
Reputation: 1004
There is some example code in this SO post that you may find useful: play apple music songs by third party applications. I was able to activate the player and play a song with:
#import <MediaPlayer/MediaPlayer.h>
[[MPMusicPlayerController systemMusicPlayer] setQueueWithStoreIDs:tracks];
[[MPMusicPlayerController systemMusicPlayer] play];
Also, the Apple Music API documentation is introduced here:
Apple Music Best Practices for App Developers
I hope it helps.
Upvotes: 0