• Stars
    star
    257
  • Rank 153,568 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

More Repositories

1

TextFieldEffects

Custom UITextFields effects inspired by Codrops, built using Swift
Swift
5,939
star
2

Cacher

Super simple caching solution for iOS, macOS, tvOS and watchOS.
Swift
173
star
3

Bike-Compass

iOS and ο£Ώ Watch app to find city bicycles to rent in your city
Swift
80
star
4

Animator

Animator helper class for Titanium Appcelerator.
JavaScript
58
star
5

Titanium-Booker

Titanium port of the wonderful Laker Compendium
JavaScript
53
star
6

UpvoteControl

UIControl for up vote widgets found in Product Hunt and Reddit
Swift
52
star
7

Instagram-For-iPad

Instagram client for iPad. Built using Titanium Alloy from Appcelerator
JavaScript
44
star
8

HuntingKit

Wrapper for the ProductHunt API. Written in Swift
Swift
44
star
9

alloy-widgets

Repository of my Titanium Alloy Widgets
JavaScript
30
star
10

Rater-Module

Create a cycling reminder to go rate your app at the App Store.
JavaScript
28
star
11

technical-articles

Just a place where I can store demo projects for my technical articles.
Swift
27
star
12

InfiniteTableView

A cross platform infinite tableview very similar to Instagram built using Appcelerator Titanium
CoffeeScript
21
star
13

ImageGalleryView

Multiplatform ImageGalleryView for Titanium Appcelerator
CoffeeScript
19
star
14

Remote-Form-Helpers

AJAX Form Helpers for ColdFusion on Wheels
ColdFusion
14
star
15

ForecastView

Extensible View subclass that displays the weather forecast of a given CLLocationCoordinate2D. Written in Swift ❀️
Swift
14
star
16

ColdFusion-Sugar

ColdFusion Sugar for MacRabbit's Espresso
13
star
17

MemPool-Module

MemPool is a module created to simplify the debugging of application performance in Appcelerator's Titanium
JavaScript
11
star
18

Scaffold

Scaffold plugin for ColdFusion on Wheels
ColdFusion
11
star
19

ghost-storage-supabase

Supabase storage adapter for Ghost. Recommended as a completely free storage solution for blogs being hosted on platforms with ephemeral filesystems, like Heroku.
JavaScript
10
star
20

probot-minimum-reviews

enforce a minimum number of reviews on Pull Requests
JavaScript
10
star
21

react-native-window-tint-color

React Native Module to globally tint the keyWindow in an iOS application
Java
9
star
22

Overlay-HUD

A simple Overlay HUD Module for Titanium following CommonJS best practices
JavaScript
8
star
23

WTFamous-App

Titanium Appcelerator application mimicking a magic 8 ball game with famous characters
JavaScript
8
star
24

Localizer

Localizer plugin for ColdFusion on Wheels
ColdFusion
7
star
25

HuntingCompanion

ProductHunt example app using Swift and the HuntingKit framework.
Swift
6
star
26

probot-duplicator

Create full copies of issues with a single command
JavaScript
6
star
27

EmojiStickers

Everything http://www.raulriera.com/emoji
Swift
6
star
28

Transactional-Email-Designer

Responsive transactional HTML email designer
JavaScript
6
star
29

PillLabel

UILabel subclass with support for border color, border radius and padding. All accesible via de Inspector window
Objective-C
4
star
30

BlankStateViewController

Convenience view controller to hide content on errors, present a loading message or simply have something there if you have nothing else to display
Objective-C
4
star
31

LoadingButton

Dead simple UIButton with a companion UIActivityIndicator written in Swift
Swift
3
star
32

Antitabaco-App

Small iOS application to report any establishment not following the anti tobacco law in Spain
JavaScript
3
star
33

Mobile-Helpers

Mobile Helper functions for ColdFusion on Wheels
1
star
34

Bootstrap-Dropdown-Extension

Easily enable content switching in a Bootstrap dropdown
JavaScript
1
star