gpt4 book ai didi

ios - 从我的 iOS 应用打开 Apple Music

转载 作者:技术小花猫 更新时间:2023-10-29 10:17:03 24 4
gpt4 key购买 nike

上下文:我目前正在开发一个与 Apple Music API 集成的 iOS 应用程序。我可以搜索歌曲并在我的应用程序上播放它们。为了尊重他们的政策,我必须允许用户在 Apple Music 应用程序上启动和播放歌曲。 Shazam 为 Apple Music、Deezer 和 Google Play 做到了这一点(见下图)。

enter image description here

我已经使用 this 为 Spotify 做到了这一点线程,基本上使用 URL 方案。在 this Reddit 线程我找到了 iOS URL Scheme 的列表,但我找不到有关 Apple Music 的任何信息 – this同一线程上的请求确认它仍然不可用。

Apple Music API 也不走运。

问题:有人知道如何使用 Apple Music 完成相同的行为吗?顺便说一句,它不一定需要使用 URL 方案。

此处的目标是启动 Apple Music 并将歌曲播放到 Apple Music 应用程序中,而不是在我的应用程序中。我已经在我的应用程序上播放 Apple Music 中的歌曲。

我是否遗漏了 API 文档中的某些内容?任何想法将不胜感激。

最佳答案

您应该使用深层链接以在 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"]];

关于ios - 从我的 iOS 应用打开 Apple Music,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40517057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com