GithubHelp home page GithubHelp logo

pescarcena / authenticationviewcontroller Goto Github PK

View Code? Open in Web Editor NEW

This project forked from raulriera/authenticationviewcontroller

0.0 2.0 0.0 171 KB

A simple to use, standard interface for authenticating to oauth 2.0 protected endpoints via SFSafariViewController.

License: MIT License

Ruby 25.07% Swift 70.97% Objective-C 3.96%

authenticationviewcontroller's Introduction

AuthenticationViewController

A simple to use, standard interface for authenticating to OAuth 2.0 protected endpoints via SFSafariViewController.

Step1

Instructions

In order to use this View Controller you need to be running iOS 9 on your simulator or device.

Step 1

Setup 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)

Step1

Step 2

Prepare your AppDelegate to handle this newly created URL Scheme

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
}

Note 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.

Step 3

Create an AuthenticationProvider following the AuthenticationProvider protocol.

Step 4

Instantiate an AuthenticationViewController in your code and pass in the provider.

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)

That 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.

Installation

Choose one of the following options.

Carthage

Add the following to your Cartfile:

github "raulriera/AuthenticationViewController"

CocoaPods

Add the following to your Podfile:

pod "AuthenticationViewController"

Manual

Just drag and drop the AuthenticationViewController/AuthenticationViewController folder into your project.

Example

Sometimes 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.

Created by

Raul Riera, @raulriera

authenticationviewcontroller's People

Contributors

raulriera avatar readmecritic avatar regnerjr avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.