AmuseKit

一个 Swift 包,用于简化 iOS、MacOS、tvOS 和 watchOS 项目中 Apple Music API 的集成。

用法

导入

import AmuseKit

初始化 AmuseKit 提供程序。

您可以使用 StorageConfiguration 初始化 AmuseKit,该配置指定了服务名称和密钥,这些密钥将用于在钥匙串上保存开发者令牌和用户令牌。

let configuration = AmuseKit.StorageConfiguration(serviceName: "KEYCHAIN_SERVICE_NAME",
                                                  developerTokenKey: "DEV_TOKEN_KEYCHAIN_KEY",
                                                  userTokenKey: "USER_TOKEN_KEYCHAIN_KEY")
let amuseProvider = AmuseKit.DataProvider(configuration)
amuseProvider.setDeveloperToken("YOUR_DEV_TOKEN")

设置用户令牌。

amuseProvider.setUserToken("USER_TOKEN")

设置用户国家代码。

amuseProvider.setUserCountryCode("USER_COUNTRY_CODE")

通过 ID 检索 Apple Music 目录资源。

支持的值包括:albums, artists, musicVideos, playlists, songs

let response = try await amuseProvider.catalog(.albums, ids: ["123", "456", "789"])
print(response.data)
amuseProvider.catalog(.albums, ids: ["123", "456", "789"])
    .sink { _ in
    } receiveValue: { response in
        print(response.data)
    }

在 Apple Music 目录中搜索。

let response = try await amuseProvider.catalogSearch(searchTerm: "YOUR_QUERY_TEXT")
print(response.results?.albums)
print(response.results?.artists)
print(response.results?.musicVideos)
print(response.results?.playlists)
print(response.results?.songs)
print(response.results?.stations)
amuseProvider.catalogSearch(searchTerm: "YOUR_QUERY_TEXT")
    .sink { _ in
    } receiveValue: { response in
        // content will be found under results properties.
        print(response.results?.albums)
        print(response.results?.artists)
        print(response.results?.musicVideos)
        print(response.results?.playlists)
        print(response.results?.songs)
        print(response.results?.stations)
    }

在 Apple Music 目录中搜索特定资源类型。

let response = try await amuseProvider.catalogSearch([.playlists, .songs], searchTerm: "YOUR_QUERY_TEXT")
print(response.results?.playlists)
print(response.results?.songs)
amuseProvider.catalogSearch([.playlists, .songs], searchTerm: "YOUR_QUERY_TEXT")
    .sink { _ in
    } receiveValue: { response in
        print(response.results?.playlists)
        print(response.results?.songs)
    }

检索用户资料库资源。

支持的值包括:albums, artists, musicVideos, playlists, songs

let response = try await dataProvider.library(.albums)
print(response.data)
amuseProvider.library(.albums)
    .sink { _ in
    } receiveValue: { response in
        print(response.data)
    }

在用户资料库中搜索。

let response = try await amuseProvider.librarySearch(searchTerm: "YOUR_QUERY_TEXT")
print(response.results?.albums)
print(response.results?.artists)
print(response.results?.musicVideos)
print(response.results?.playlists)
print(response.results?.songs)
amuseProvider.librarySearch(searchTerm: "YOUR_QUERY_TEXT")
    .sink { _ in
    } receiveValue: { response in
        // content will be found under results properties.
        print(response.results?.albums)
        print(response.results?.artists)
        print(response.results?.musicVideos)
        print(response.results?.playlists)
        print(response.results?.songs)
    }

在用户资料库中搜索特定资源类型。

let response = try await amuseProvider.librarySearch([.playlists, .songs], searchTerm: "YOUR_QUERY_TEXT")
print(response.results?.playlists)
print(response.results?.songs)
amuseProvider.librarySearch([.playlists, .songs], searchTerm: "YOUR_QUERY_TEXT")
    .sink { _ in
    } receiveValue: { response in
        print(response.results?.playlists)
        print(response.results?.songs)
    }