测试已测试 | ✗ |
Lang语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2017年10月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由Raul Riera维护。
这是一个简单易用、标准接口,用于通过SFSafariViewController对OAuth 2.0受保护的端点进行认证。
为了使用此视图控制器,您需要在您的模拟器或设备上运行iOS 9。
按照以下图片设置您的应用程序的URL方案。(您可以在项目设置的“信息”标签中找到此设置)
准备您的 AppDelegate
以处理新创建的URL方案
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
// Before doing this, you should check the url is your redirect-uri before doing anything. Be safe :)
if let components = NSURLComponents(URL: url, resolvingAgainstBaseURL: false),
queryItems = components.queryItems,
code = queryItems.first?.value {
// Let's find the instance of our authentication controller,
// it would be the presentedViewController. This is another
// reason to check before that we are actually coming from the SFSafariViewController
if let rootViewController = window?.rootViewController,
authenticationViewController = rootViewController.presentedViewController as? AuthenticationViewController {
authenticationViewController.authenticateWithCode(code)
}
return true
}
return false
}
请注意,您需要将接收到的 认证代码
传递给 AuthenticationViewController
,以便它可以进行交换以获取实际的 访问令牌
。
按照要求创建一个 AuthenticationProvider
。
在您的代码中创建一个 AuthenticationViewController
实例,并传入提供者。
let provider = OAuthDribbble(clientId: "your-client-id",
clientSecret: "your-client-secret",
scopes: ["public", "upload"])
let authenticationViewController = AuthenticationViewController(provider: provider)
authenticationViewController.failureHandler = { error in
print(error)
}
authenticationViewController.authenticationHandler = { token in
print(token)
authenticationViewController.dismissViewControllerAnimated(true, completion: nil)
}
presentViewController(authenticationViewController, animated: true, completion: nil)
这就完成了,当您在 AuthenticationViewController
中填写用户帐户且一切顺利时,您应该在 authenticationHandler
闭包中获取 访问令牌
。否则,请检查 failureHandler
闭包中的任何错误。
选择以下选项之一。
只需将 AuthenticationViewController/AuthenticationViewController
文件夹拖放到您的项目中。
有时深入研究一些代码更容易,这个项目中包含了一些用于Dribbble和Instagram的示例。您仍然需要编辑源代码,以提供实际的 clientId
、clientSecret
和您的 URL 方案
。
Raul Riera, @raulriera