SpotifyKit 0.0.6

SpotifyKit 0.0.6

测试测试
Lang语言 SwiftSwift
许可证 MIT
发布最新发布2018年9月
SPM支持 SPM

John Doe 维护。



  • 作者
  • John Doe

SpotifyKit

Spotify 的 Web API 的 Swift 客户端。

Build Status Version License Platform

安装

SpotifyKit 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile

pod "SpotifyKit"

初始化

您可以通过提供 Spotify 应用程序数据来轻松创建一个 SpotifyKit 辅助对象。

let spotifyManager = SpotifyManager(with:
    SpotifyManager.SpotifyDeveloperApplication(
        clientId:     "client_id",
        clientSecret: "client_secret",
        redirectUri:  "redirect_uri"
    )
)

在认证时刻收集的令牌数据会自动保存到 Keychain 的安全首选项中。

认证

这是极具挑战性的一步。在您的应用程序启动时,您应该像这样调用授权方法:

spotifyManager.authorize()

它向用户的账号发送一个授权请求,这将会产生一个带有指定URL前缀和授权码参数的HTTP响应。如果找到一个已保存的令牌,该方法会自动跳过这个过程。

然后,为了完成认证并获得令牌,您必须在您的应用Info.plist文件中设置一个URL方案,并像这样捕获代码

MacOS

/**
 Registers the URL watcher
 */
NSAppleEventManager.shared().setEventHandler(self,
    andSelector: #selector(handleURLEvent),
    forEventClass: AEEventClass(kInternetEventClass),
    andEventID: AEEventID(kAEGetURL))

/**
 Catches URLs with specific prefix ("your_spotify_redirect_uri://")
 */
func handleURLEvent(event: NSAppleEventDescriptor,
                    replyEvent: NSAppleEventDescriptor) {
	if	let descriptor = event.paramDescriptor(forKeyword: keyDirectObject),
		let urlString  = descriptor.stringValue,
		let url        = URL(string: urlString) {
		spotifyManager.saveToken(from: url)
	}
}

现在SpotifyKit已经通过您的用户账号完全认证,您可以使用它提供的所有方法。

使用方法

所有方法都通过URLSession API发送HTTP请求,并使用简单的回调报告结果。

查找

在Spotify数据库中查找曲目(就像这个示例一样)、专辑或艺术家

// Signature
public func find<T>(_ what: T.Type,
                    _ keyword: String,
                    completionHandler: @escaping ([T]) -> Void) where T: SpotifySearchItem

// Example
spotifyManager.find(SpotifyTrack.self, "track_title") { tracks in
	// Tracks is a [SpotifyTrack] array
	for track in tracks {
        print("URI:    \(track.uri), "         +
              "Name:   \(track.name), "        +
              "Artist: \(track.artist.name), " +
              "Album:  \(track.album.name)")
    }
}

get()和library()函数也可用于检索特定项目或获取您的音乐库中的曲目、专辑或播放列表。

用户音乐库交互

保存、删除并检查“您的音乐”播放列表中曲目的保存状态

// Save the track
spotifyManager.save(trackId: "track_id") { saved in
    print("Saved: \(saved)")
}

// Check if the track is saved
spotifyManager.isSaved(trackId: "track_id") { isSaved in
    print("Is saved: \(isSaved)")
}

// Delete the track
spotifyManager.delete(trackId: "track_id") { deleted in
    print("Deleted: \(deleted)")
}

支持的Spotify项目

这些项目将自动从JSON解码,遵循Swift 4的“Decodable”协议。

通用项目

由所有项目继承的协议,包括公共属性

public protocol SpotifyItem: Decodable {

	var id:   String { get }
	var uri:  String { get }
	var name: String { get }
}

public protocol SpotifySearchItem: SpotifyItem {
	// Items conforming to this protocol can be searched
}

public protocol SpotifyLibraryItem: SpotifyItem {
	// Items conforming to this protocol can be contained in user's library
}

用户

public struct SpotifyUser: SpotifySearchItem {

	public var email:  String?

	// URI of the user profile picture
	public var artUri: String
}

曲目

public struct SpotifyTrack: SpotifySearchItem, SpotifyLibraryItem {

	public var album:  SpotifyAlbum?
	public var artist: SpotifyArtist
}

专辑

public struct SpotifyAlbum: SpotifySearchItem, SpotifyLibraryItem, SpotifyTrackCollection {

	// The tracks contained in the album
	public var collectionTracks: [SpotifyTrack]?

	public var artist: SpotifyArtist

	// The album's cover image
	public var artUri: String
}

播放列表

public struct SpotifyPlaylist: SpotifySearchItem, SpotifyLibraryItem, SpotifyTrackCollection {

	// The tracks contained in the playlist
	public var collectionTracks: [SpotifyTrack]?
}

艺术家

public struct SpotifyArtist: SpotifySearchItem {
	// Artist has no additional properties
}

示例

  • iOS和macOS示例项目存放在这个仓库中
  • Muse