Coder Social home page Coder Social logo

oauth2-demo-ios's Introduction

OAuth 2.0 Demo for iOS

This is a simple iPhone app demonstrating how to manually use the ArcGIS OAuth 2.0 API to obtain an access token for a user.

When the user clicks the "Sign In" button, the app launches a browser to the ArcGIS authorization endpoint.

    NSURL *url = [NSURL URLWithString:@"https://www.arcgis.com/sharing/oauth2/authorize?"
                  "response_type=token&"
                  "client_id=eKNjzFFjH9A1ysYd&"
                  "redirect_uri=oauthdemo://auth"];
    [[UIApplication sharedApplication] openURL:url];

The app sets up a custom protocol handler so that when Safari redirects after authorization is complete, the app is launched by the oauthdemo://auth URL.

When the user finishes signing in, the app is re-launched with the token information appended to the redirect URI, which looks something like the following:

oauthdemo://auth#access_token=lS0KgilpRsT07qT_iMOg9bBSaWqODC1g061nSLsa8gV2GYtyynB6A-
abCsWrDTvN9p7rI0kWa4u-ORXuFUQ7QGxiiniwpCSIV1AqzoLRHF1hYcI4joeDPOzZa9PZigiudtefciZy5
&expires_in=7199&username=guest

The app then parses this out of the query string and stores the token and username in the app's preferences file.

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if([[url host] isEqualToString:@"auth"]) {
        NSDictionary *params = [self parseQueryString:[[url fragment] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

        // Store the access token
        [[NSUserDefaults standardUserDefaults] setObject:[params objectForKey:@"access_token"] forKey:OADTokenDefaultsName];

        // Calculate the expiration date so we know if the token is invalid
        NSDate *expDate = [NSDate dateWithTimeIntervalSinceNow:[[params objectForKey:@"expires_in"] integerValue]];
        [[NSUserDefaults standardUserDefaults] setObject:expDate forKey:OADTokenExpirationDefaultsName];

        // Store the username
        [[NSUserDefaults standardUserDefaults] setObject:[params objectForKey:@"username"] forKey:OADUsernameDefaultsName];

        // Save
        [[NSUserDefaults standardUserDefaults] synchronize];

        // Notify the view that a new token is available
        [[NSNotificationCenter defaultCenter] postNotificationName:OADNewTokenAvailable object:self];
    }    
    return YES;
}

The app can now continue to operate by using the access token wherever is needed!

oauth2-demo-ios's People

Contributors

aaronpk avatar

Stargazers

Nur Sarowar avatar  avatar sean h. avatar Song Zhou avatar John van de Water avatar bill avatar

Watchers

Tom avatar James Cloos avatar  avatar  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.