OAuth 2.0 integration in iOS

26,263

In almost all projects I have used AFNetworking because it's very powerful -why re-invent the wheel every time :)

Furthermore, it also has an OAuth2Manager which is quite easy to implement and works rock-solid.

Share:
26,263

Related videos on Youtube

Supertecnoboff
Author by

Supertecnoboff

I’m Daniel Sadjadian - Car enthusiast, computer programmer & entrepreneur running my own business. I live life to the max and try my best to get the most out of each day. Every moment counts :)

Updated on August 11, 2020

Comments

  • Supertecnoboff
    Supertecnoboff almost 4 years

    I have been trying for ages now to get OAuth 2.0 integration in my iPhone application.

    I have searched and searched for libraries, tutorials, etc... But they have all lead me to a dead end. The main problem I have is that they either have deprecated code, or they just don't work or they have some documentation but its really hard to follow (for me anyway...).

    The best OAuth2 library I could find for Xcode is this one: https://github.com/nxtbgthng/OAuth2Client

    But the main problem with that one is it doesn't seem to do anything... I have followed all the documentation and instructions that came with it, but after building and running, it doesn't seem to authenticate....

    So I guess my main question is: does anyone know of any good and up to date OAuth 2.0 tutorials for Xcode or any libraries for such a thing?

    Because I am really struggling at the moment.... :(

    Thanks for your time, Dan.

    UPDATE 2: Here is my code (App Id and secret removed for security):

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
    oauthClient = [[LROAuth2Client alloc]
                   initWithClientID:@"MY_CLIENT_ID"
                   secret:@"MY_APP_SECRET"
                   redirectURL:[NSURL URLWithString:@"app://instagram-callback/?code="]];
    oauthClient.delegate = self;
    
    oauthClient.userURL  = [NSURL URLWithString:@"https://api.instagram.com/oauth/authorize/?client_id=ab6dc96859bf43b3a488199ec72d9964&redirect_uri=app://instagram-callback/?code=&response_type=code"];
    oauthClient.tokenURL = [NSURL URLWithString:@"https://api.instagram.com/oauth/access_token/"];
    
    
    [oauthClient authorizeUsingWebView:myWebView];
    
    
    }
    
    - (void)oauthClientDidReceiveAccessToken:(LROAuth2Client *)client;
    {
        LROAuth2AccessToken *token = client.accessToken;
        [NSKeyedArchiver archiveRootObject:token toFile:@"Path/To/MyAccessToken"];
    }
    
    - (void)checkAccessTokenForExpiry:(LROAuth2AccessToken *)accessToken;
    {
        if ([accessToken hasExpired]) {
            [oauthClient refreshAccessToken:accessToken];
        }
    }
    
    - (void)oauthClientDidRefreshAccessToken:(LROAuth2Client *)client;
    {
        LROAuth2AccessToken *token = client.accessToken;
        [NSKeyedArchiver archiveRootObject:token toFile:@"Path/To/MyAccessToken"];
    }
    
    • sbarow
      sbarow over 10 years
      Where is it failing? I assume this is your issue you posted. github.com/nxtbgthng/OAuth2Client/issues/82 Does it present a modal view/safari to authenticate with Instagram? or is it a case of literally clicking login and nothing happens?
    • sbarow
      sbarow over 10 years
      Answered in your github issue.
    • Supertecnoboff
      Supertecnoboff over 10 years
      @sbarow Thank you for your help. I have a left a comment on the Github page where I posted the issue. Please have a look at it, as the app crashes because it can't get a token.
    • sbarow
      sbarow over 10 years
      Would you mind pasting the code you are using? Maybe use gist?
    • Supertecnoboff
      Supertecnoboff over 10 years
      @sbarow Sure. I have updated the post with the code. Thank you for your help.
    • Supertecnoboff
      Supertecnoboff over 10 years
      @sbarow The problem now is that my UIWebView doesn't seem to change.... I don't know really. For example, I think the app does now get the token but it doesn't react to the Redirect URL.
    • sbarow
      sbarow over 10 years
      When you say the web view doesn't change? So does it now show a view allowing the user to log in and then once they have logged in it doesn't go away (redirect) ? I think you need to handle dismissing the webview. Have a look at the UIWebView delegate methods, maybe one of them will help you know when to dismiss the view developer.apple.com/library/ios/documentation/uikit/referenc‌​e/…
    • Supertecnoboff
      Supertecnoboff over 10 years
      @sbarow I'm not sure if those methods can help me. Basically, my problem is that my app doesn't seem to register the RedirectURL. So it does present the UIWebView and the user can login, but when they then press authorise in the UIWebView, nothing happens.... (When pressing Authorise the service will provide you or redirect you to your specified RedirectURL... but my app doesn't seem to react to it....
    • sbarow
      sbarow over 10 years
      Taking a stab at this, Facebook SDK requires you to edit your .plist developers.facebook.com/docs/getting-started/… 5. Configure a new Xcode Project. look for Configure the .plist I have no idea if that could help.
    • Supertecnoboff
      Supertecnoboff over 10 years
      @sbarow Probably not in this case as Im trying to connect to the Instagram API.
    • sbarow
      sbarow over 10 years
      I would look into this, authorizationURLWithRedirectURL:[NSURL URLWithString:@"myapp://instagram-callback"];
  • Maxime Epain
    Maxime Epain over 8 years
    Yes! I've used the AFOAuth1Client and it was pretty easy to integrate. Probably the same with OAuth2Manager!