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"]];