AuthenticationViewController 1.3.0

AuthenticationViewController 1.3.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2017年10月
SwiftSwift 版本3.0
SPM支持 SPM

Raul Riera维护。



AuthenticationViewController

这是一个简单易用、标准接口,用于通过SFSafariViewController对OAuth 2.0受保护的端点进行认证。

Step1

说明

为了使用此视图控制器,您需要在您的模拟器或设备上运行iOS 9。

步骤 1

按照以下图片设置您的应用程序的URL方案。(您可以在项目设置的“信息”标签中找到此设置)

Step1

步骤 2

准备您的 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,以便它可以进行交换以获取实际的 访问令牌

步骤 3

按照要求创建一个 AuthenticationProvider

步骤 4

在您的代码中创建一个 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的示例。您仍然需要编辑源代码,以提供实际的 clientIdclientSecret 和您的 URL 方案

创建者:

Raul Riera, @raulriera