user60312321 user60312321 - 1 year ago 118
iOS Question

Opening Apple Music from my iOS app

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).

enter image description here

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 look 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.

Answer Source

You should use deeplinks in order to open tacks in Apple Music app:

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 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: // 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"]];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download