测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可 | MIT |
发布最新版本 | 2016年1月 |
SPM支持 SPM | ✗ |
由 MATSUMOTO Yuji 维护。
依赖 | |
APIKit | ~> 1.1.1 |
Starscream | ~> 1.0.2 |
TypetalkKit 是 Typetalk 的非官方网络 API 客户端。
以下代码获取用户的主题并打印出来。
TypetalkAPI.sendRequest(GetTopics()) { result in
switch result {
case .Success(let ts):
for i in ts {
print(i)
}
case .Failure(let error):
// error
}
}
TypetalkKit 支持几乎所有的 Typetalk API。
如果您想了解有关 API 的更多信息,请访问 官方页面。
要使用 TypetalkKit,您首先需要在 官方开发者页面 注册您的应用程序。
当您注册应用程序时,选择“授权码”作为授权类型,并设置包括自定义 URL 方案的 URI,完成授权过程。
在您的应用程序中设置开发者设置
TypetalkAPI.setDeveloperSettings(
clientId: "Your ClientID",
clientSecret: "Your SecretID",
redirectURI: "Your custome scheme", // e.g. typetalkkit+<YOUR_APP_ID>://auth/success
scopes: [Scope.my, Scope.topic_read])
然后调用 authorize
。
TypetalkAPI.authorize { (error) -> Void in
...
}
TypetalkKit 使用 Safari 进行 Typetalk 的授权过程。因此,您需要将一个自定义 URL 方案添加到您的 Info.plist
中的“重定向 URI”,以便切换回您的应用程序。
当您的应用程序从 Safari 返回时,在您的 application(openURL, sourceApplication, annotation)
中调用 TypetalkAPI.authorizationDone
,如下所示
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
if TypetalkAPI.isRedirectURL(url) && sourceApplication? == "com.apple.mobilesafari" {
return TypetalkAPI.authorizationDone(URL: url)
}
return false
}
如果您没有调用 authorizationDone
,则 authorize
的回调永远不会被调用。
在 OS X 中,您可以在 handleGetURLEvent
中使用 TypetalkAPI.authorizationDone
,如下所示
func handleGetURLEvent(event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
if let ev = event,
url_str = ev.descriptorForKeyword(AEKeyword(keyDirectObject))?.stringValue,
url = NSURL(string: url_str) where TypetalkAPI.isRedirectURL(url) {
TypetalkAPI.authorizationDone(URL: url)
}
}
有关 Typetalk 授权的更多信息,请参阅 官方页面。
将 TypetalkKit 添加为子模块
git submodule add https://github.com/safx/TypetalkKit.git
将 TypetalkKit.xcodeproj 添加到您的项目中。