使用 Swift 编写的 Disqus API 包装器。此开源库允许您将其集成到 iOS 应用程序中。了解有关 Disqus APIs 的更多信息。
DisqusService 可通过 CocoaPods 获取。要安装它,只需在 Podfile 中添加以下行
pod 'DisqusService'
在您的 AppDelegate
类中添加以下代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
DisqusService.shared.set(publicKey: "<your-public-key>",
secretKey: "<your-secret-key>",
redirectURI: "<your-redirect-uri>")
}
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if let sourceApplication = options[.sourceApplication] as? String {
if sourceApplication == "com.apple.SafariViewService" {
NotificationCenter.default.post(name: .DisqusServiceSafariAuthDidClose, object: url)
}
}
return true
}
由于 Disqus API 不支持在 OAuth 身份验证后重定向应用的 schemes,您必须提供重定向到以下 scheme 的 URL:disqus-redirect://
。在项目设置 → 信息中,向下滚动到 URL 类型,并添加一个带有 +
的条目
com.disqusService.testApp
disqus-redirect
用户认证/注销操作非常简单
DisqusService.shared.authenticate(viewController: yourVC) { (success) in
if success {
} else {
}
}
////////////////
DisqusService.shared.logout()
此库还包括一个方便的类名 DisqusComment,用于更好地处理从线程接收到的评论。以下是一个示例
let params = ["thread" : "<your-thread-id>", "order" : "asc"]
let posts: [DisqusComment] = []
DisqusService.shared.performGETRequest(api: "posts/list", authRequired: false, params: params) { (data, success) -> Void in
if success {
let posts = data?["response"] as! [[AnyHashable : Any]]
for postJSON in posts {
if let post = DisqusComment(disqusData: postJSON) {
posts.append(post)
}
}
}
}
请注意,某些 API 需要认证,并且必须指定。API 使用示例
let paramsDetails = ["forum" : "<your-forum>", "thread" : "<your-thread-id>"]
DisqusService.shared.performGETRequest(api: "threads/details", authRequired: false, params: paramsDetails) { (data, success) in
}
let paramsVote = ["post" : "<your-post-id>", "vote" : "1"]
DisqusService.shared.performPOSTRequest(api: "posts/vote", authRequired: true, params: paramsVote) { (data, success) in
}
Matteo Riva,[email protected]
DisqusService 根据 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。