从我的 iOS 应用打开 Apple Music
Opening Apple Music from my iOS app
Context:我目前正在开发与 Apple Music API 集成的 iOS 应用程序。我可以搜索歌曲并在我的应用程序上播放它们。为了尊重他们的政策,我必须允许用户在 Apple Music 应用程序上启动和播放歌曲。 Shazam 为 Apple Music、Deezer 和 Google Play 做到了这一点(见下图)。
我在同一线程上使用 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 请求为 Spotify 做了这件事,确认它仍然不可用。
Apple Music API 也不走运。
问题:有人知道如何使用 Apple Music 实现相同的行为吗?顺便说一句,它不一定需要使用 URL 方案。
此处的目标是启动 Apple Music 并将歌曲播放到 Apple Music 应用程序中,而不是在我的应用程序中。我已经在我的应用程序上播放 Apple Music 中的歌曲。
我是否遗漏了 API 文档中的某些内容?任何想法将不胜感激。
此 SO post 中有一些示例代码可能对您有用:
。
我能够激活播放器并播放歌曲:
#import <MediaPlayer/MediaPlayer.h>
[[MPMusicPlayerController systemMusicPlayer] setQueueWithStoreIDs:tracks];
[[MPMusicPlayerController systemMusicPlayer] play];
另外,Apple Music API 文档介绍在这里:
Apple Music Best Practices for App Developers
希望对您有所帮助。
您应该使用深层链接才能在 Apple Music 应用程序中打开大头钉:
https://affiliate.itunes.apple.com/resources/documentation/linking-to-the-itunes-music-store/
首先,您需要通过 SKCloudServiceController API 请求授权以检查您的能力(例如,如果您的设备允许播放 Apple Music 曲目)。
[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/search 发出请求:
NSDictionary *parameters = @{
@"isStreamable" : @(YES),
@"term" : @"your search parameter"
@"media" : @"music",
@"limit" : @(5),
@"country" : @"your country code"
};
作为对此请求的响应,您将收到一组跟踪结果,其中包含许多相关参数。其中之一是 "trackViewUrl"。只需将以下参数添加到此 trackViewUrl 以使其深度链接到 Apple Music 应用程序:
NSString *appleMusicDeepLinking = [NSString stringWithFormat:@"%@&mt=1&app=music", response[0][@"trackViewUrl"]];
Context:我目前正在开发与 Apple Music API 集成的 iOS 应用程序。我可以搜索歌曲并在我的应用程序上播放它们。为了尊重他们的政策,我必须允许用户在 Apple Music 应用程序上启动和播放歌曲。 Shazam 为 Apple Music、Deezer 和 Google Play 做到了这一点(见下图)。
我在同一线程上使用 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 请求为 Spotify 做了这件事,确认它仍然不可用。
Apple Music API 也不走运。
问题:有人知道如何使用 Apple Music 实现相同的行为吗?顺便说一句,它不一定需要使用 URL 方案。
此处的目标是启动 Apple Music 并将歌曲播放到 Apple Music 应用程序中,而不是在我的应用程序中。我已经在我的应用程序上播放 Apple Music 中的歌曲。
我是否遗漏了 API 文档中的某些内容?任何想法将不胜感激。
此 SO post 中有一些示例代码可能对您有用:
#import <MediaPlayer/MediaPlayer.h>
[[MPMusicPlayerController systemMusicPlayer] setQueueWithStoreIDs:tracks];
[[MPMusicPlayerController systemMusicPlayer] play];
另外,Apple Music API 文档介绍在这里:
Apple Music Best Practices for App Developers
希望对您有所帮助。
您应该使用深层链接才能在 Apple Music 应用程序中打开大头钉: https://affiliate.itunes.apple.com/resources/documentation/linking-to-the-itunes-music-store/
首先,您需要通过 SKCloudServiceController API 请求授权以检查您的能力(例如,如果您的设备允许播放 Apple Music 曲目)。
[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/search 发出请求:
NSDictionary *parameters = @{
@"isStreamable" : @(YES),
@"term" : @"your search parameter"
@"media" : @"music",
@"limit" : @(5),
@"country" : @"your country code"
};
作为对此请求的响应,您将收到一组跟踪结果,其中包含许多相关参数。其中之一是 "trackViewUrl"。只需将以下参数添加到此 trackViewUrl 以使其深度链接到 Apple Music 应用程序:
NSString *appleMusicDeepLinking = [NSString stringWithFormat:@"%@&mt=1&app=music", response[0][@"trackViewUrl"]];