{"id":16452044,"url":"https://github.com/raulriera/authenticationviewcontroller","last_synced_at":"2025-05-08T23:43:52.301Z","repository":{"id":56902536,"uuid":"45559972","full_name":"raulriera/AuthenticationViewController","owner":"raulriera","description":"A simple to use, standard interface for authenticating to oauth 2.0 protected endpoints via SFSafariViewController.","archived":false,"fork":false,"pushed_at":"2017-10-08T17:28:07.000Z","size":177,"stargazers_count":257,"open_issues_count":4,"forks_count":14,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-05-08T23:43:46.083Z","etag":null,"topics":["authentication","oauth","sfsafariviewcontroller","swift"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/raulriera.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-04T18:47:14.000Z","updated_at":"2024-05-05T06:37:31.000Z","dependencies_parsed_at":"2022-08-21T01:50:40.097Z","dependency_job_id":null,"html_url":"https://github.com/raulriera/AuthenticationViewController","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raulriera%2FAuthenticationViewController","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raulriera%2FAuthenticationViewController/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raulriera%2FAuthenticationViewController/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/raulriera%2FAuthenticationViewController/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/raulriera","download_url":"https://codeload.github.com/raulriera/AuthenticationViewController/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253166474,"owners_count":21864467,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authentication","oauth","sfsafariviewcontroller","swift"],"created_at":"2024-10-11T10:11:09.954Z","updated_at":"2025-05-08T23:43:52.283Z","avatar_url":"https://github.com/raulriera.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AuthenticationViewController\nA simple to use, standard interface for authenticating to OAuth 2.0 protected endpoints via SFSafariViewController.\n\n![Step1](/Screenshots/Demo.gif)\n\n## Instructions\nIn order to use this View Controller you need to be running iOS 9 on your simulator or device.\n\n### Step 1\nSetup the URL Scheme of your app as shown in the image below. (You can find this in the Info tab of your project's settings)\n\n![Step1](/Screenshots/Step1.png)\n\n### Step 2\nPrepare your `AppDelegate` to handle this newly created URL Scheme\n\n```swift\nfunc application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -\u003e Bool {\n\n    // Before doing this, you should check the url is your redirect-uri before doing anything. Be safe :)\n    if let components = NSURLComponents(URL: url, resolvingAgainstBaseURL: false),\n        queryItems = components.queryItems, \n        code = queryItems.first?.value {\n\n        // Let's find the instance of our authentication controller, \n        // it would be the presentedViewController. This is another \n        // reason to check before that we are actually coming from the SFSafariViewController\n        if let rootViewController = window?.rootViewController,\n            authenticationViewController = rootViewController.presentedViewController as? AuthenticationViewController {\n            authenticationViewController.authenticateWithCode(code)\n        }\n\n        return true\n    }\n\n    return false\n}\n```\n\nNote that you need to pass the `authentication code` received by your URL scheme to the `AuthenticationViewController` so it can exchange it for an actual `access token`.\n\n### Step 3\nCreate an `AuthenticationProvider` following the `AuthenticationProvider` protocol.\n\n### Step 4\nInstantiate an `AuthenticationViewController` in your code and pass in the provider.\n\n```swift\nlet provider = OAuthDribbble(clientId: \"your-client-id\", \n    clientSecret: \"your-client-secret\", \n    scopes: [\"public\", \"upload\"])\n\nlet authenticationViewController = AuthenticationViewController(provider: provider)\n\nauthenticationViewController.failureHandler = { error in\n    print(error)\n}\n\nauthenticationViewController.authenticationHandler = { token in\n    print(token)\n\n    authenticationViewController.dismissViewControllerAnimated(true, completion: nil)\n}\n\npresentViewController(authenticationViewController, animated: true, completion: nil)\n```\n\nThat is it, when you fill in your user account in the `AuthenticationViewController` and if everything went correctly you should get the `access token` in the `authenticationHandler` closure. Otherwise check for any errors in the `failureHandler` closure.\n\n## Installation\n\nChoose one of the following options.\n\n### Carthage\n\nAdd the following to your Cartfile:\n\n``` ruby\ngithub \"raulriera/AuthenticationViewController\"\n```\n\n### CocoaPods\n\nAdd the following to your Podfile:\n\n``` ruby\npod \"AuthenticationViewController\"\n```\n\n### Manual\n\nJust drag and drop the `AuthenticationViewController/AuthenticationViewController` folder into your project.\n\n## Example\n\nSometimes it's easier to dig in into some code, included in this project is an example for Dribbble and Instagram. You will still need to edit the source code to provide real `clientId`, `clientSecret`, and your `URL scheme`.\n\n## Created by\nRaul Riera, [@raulriera](http://twitter.com/raulriera)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraulriera%2Fauthenticationviewcontroller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fraulriera%2Fauthenticationviewcontroller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fraulriera%2Fauthenticationviewcontroller/lists"}