信息
此 SDK 是围绕 官方 Twitch API 的现代、轻量级包装器。其主要目的是在构建需要与各种 Twitch 服务集成的应用程序时,使您的生活更轻松。代码是 100% Swift,不依赖于任何外部依赖。部署目标设置为 iOS 12。
安装
Twitch 通过 CocoaPods 提供。为了安装它,请简单地将以下行添加到您的 Podfile
pod 'Twitch'
先决条件
在您可以使用 SDK 之前,您需要在第 Twitch 开发者网站上注册您的应用程序。
使用方法
1. 初始化
一旦您已注册应用程序并获取到客户端ID,就可以开始了。要初始化SDK,请在应用程序的application(_:didFinishLaunchingWithOptions:)
(或相关场景代理方法)中放入以下代码。
// A configuration with redirect uri and scope(s) needs to be specified
let config = TWConfig(redirectUri: "REDIRECT_URI", scopes: [TWConfig.Scope.openid])
// Initialize the Twitch object with client ID and configuration
Twitch.initialize(clientId: "CLIENT_ID", config: config)
2. 获取访问令牌
2.1 OAuth令牌(隐式流程)
获取OAuth 2.0访问令牌最简单的方法是使用包含的TWAuthViewController
。
final class MyViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// This presents a web view where the user can login to his/her Twitch account.
// After a successful login, the access token is stored automatically by the SDK
// and you can proceed to calling the Twitch API.
present(TWAuthViewController(delegate: self), animated: true, completion: nil)
}
}
// If you want a callback when the access token has been successfully retrieved,
// you can implement the (optional) TWOAuthDelegate method below.
extension MyViewController: TWOAuthDelegate {
func didFetchToken(_ accessToken: String) {
print("Got an OAuth 2.0 token: \(accessToken)")
}
}
3. 调用API
以下示例代码将检索Twitch上当前观众数最多的所有游戏。
Twitch.Games.getTopGames { result in
// Note! It's safe to call UI updates from this block
switch result {
case .success(let container):
for game in container.data {
print(game)
}
break
case .failure(let error):
print(error.localizedDescription)
}
}
// This prints the following:
// TWGame(id: "509658", name: "Just Chatting", boxArtUrl: "...")
// TWGame(id: "21779", name: "League of Legends", boxArtUrl: "...")
// TWGame(id: "33214", name: "Fortnite", boxArtUrl: "...")
// ...
可用的API调用
API方法 | Swift函数 | 支持? |
---|---|---|
获取Cheermotes | Twitch.Bits.getCheermotes | |
获取Bits排行榜 | Twitch.Bits.getBitsLeaderboard | |
获取游戏分析 | Twitch.Analytics.getGameAnalytics | |
获取扩展事务 | Twitch.Extensions.getTransactions | |
创建剪辑 | Twitch.Clips.createClip | |
获取剪辑 | Twitch.Clips.getClips | |
创建授权授予上传URL | Twitch.Entitlements.createGrantsUploadUrl | |
获取代码状态 | Twitch.Entitlements.getCodeStatus | |
兑换代码 | Twitch.Entitlements.redeemCode | |
获取顶级游戏 | Twitch.Games.getTopGames | |
获取游戏 | Twitch.Games.getGames | |
检查自动化Moderation状态 | Twitch.Moderation.checkAutomodStatus | |
获取被封禁用户 | Twitch.Moderation.getBannedUsers | |
获取被封禁事件 | Twitch.Moderation.getBannedEvents | |
获取管理员 | Twitch.Moderation.getModerators | |
获取管理员事件 | Twitch.Moderation.getModeratorEvents | |
搜索类别 | 火狐搜索类别名搜索类目 | |
搜索频道 | 火狐搜索搜索频道 | |
获取流密钥 | 火狐流获取流密钥 | |
获取流 | 火狐流获取流 | |
创建流标记 | 火狐流创建流标记 | |
获取流标记 | 火狐流获取流标记 | |
获取频道信息 | 火狐流获取频道信息 | |
修改频道信息 | 火狐流修改频道信息 | |
获取主播订阅 | 火狐订阅获取主播订阅 | |
获取所有流标签 | 火狐标签获取所有流标签 | |
获取流标签 | 火狐标签获取流标签 | |
替换流标签 | 火狐标签替换流标签 | |
创建用户关注 | 火狐用户创建用户关注 | |
删除用户关注 | 火狐用户删除用户关注 | |
获取用户 | 火狐剪辑获取用户 | |
获取用户关注 | 火狐用户获取关注 | |
更新用户 | 火狐剪辑更新用户 | |
获取用户扩展 | 火狐用户获取扩展 | |
获取用户活动扩展 | 火狐用户获取用户活动扩展 | |
更新用户扩展 | 火狐用户更新用户扩展 | |
获取视频 | 火狐视频获取视频 | |
获取Webhook订阅 | 火狐订阅获取Webhook订阅 | |
获取狂欢列车事件 | 火狐狂欢列车获取狂欢列车事件 |
授权
火狐遵循MIT许可。有关更多信息,请参阅LICENSE文件。