Muhammad Umar
Muhammad Umar

Reputation: 11782

Redirecting Url to app in iphone for linkedIn

I am trying to authorize LinkedIn with iPhone..

I am using following code to redirect url

NSString *authUrl = [NSString stringWithFormat:@"https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=%@&scope=%@&state=%@&redirect_uri=%@" ,
                     API_KEY ,
                     @"r_fullprofile",
                     @"ASDKASIIWER23432KKQ",
                     @"http://www.myappname.com"
                     ];


[[UIApplication sharedApplication] openURL:[NSURL URLWithString: authUrl]];

In my Url types, i have added URL Scheme as http:// and url identifier as

 www.myappname.com

however after authorizing , i don't get back to my application from browser.

Any ideas where i am wrong?

Upvotes: 1

Views: 5277

Answers (2)

Muhammad Umar
Muhammad Umar

Reputation: 11782

i have used a diff approach now, i have used webView inside app and is using it. Works perfect so far.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.myWebView setDelegate:self];
    self.indicator = [[CustomActivityViewer alloc] initWithView:self.view];

    NSString *authUrl = [NSString stringWithFormat:@"https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=%@&scope=%@&state=%@&redirect_uri=%@" ,
                         API_KEY ,
                         @"r_fullprofile rw_nus r_emailaddress r_network w_messages",
                         SCOPE_CODE
                         REDIRECT_URI
                         ];
    authUrl = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:authUrl]]];
}

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [self.indicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.indicator stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self.indicator stopAnimating];
}

- (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
{
    NSURL *url = request.URL;
    NSLog(@"%@", url.absoluteString);

    if ( [url.host isEqualToString:HOST])
    {
        URLParser *parser = [[URLParser alloc] initWithURLString:url.absoluteString];
        NSString *code = [parser valueForVariable:@"code"];

        if (code != nil)
        {
            NSString *authUrl = [NSString stringWithFormat:@"https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=%@&redirect_uri=%@&client_id=%@&client_secret=%@",
                                 code,
                                 REDIRECT_URI_OAUTH,
                                 API_KEY,
                                 SECRET_KEY];

            NSLog(@"%@" , authUrl);
            authUrl = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

           [Utilities responseFromURL:[NSURL URLWithString:authUrl] completionBlock:^(NSString *response, NSError *err)
            {
                if (err != nil)
                {
                    [Utilities errorDisplay];
                }
                else
                {
                    NSDictionary *results = [response JSONValue];
                    [defaults setObject:[results objectForKey:@"access_token"] forKey:@"access_token"];
                }
           }];
        }
    }
    return YES;
}

Upvotes: 3

gaige
gaige

Reputation: 17481

You are going to need to register a custom URL scheme, as iOS and OS X don't have a way to redirect a specific host within a URL scheme.

Generally, you can use something like x-myapp: as the url scheme.

Further, the URL Identifier is not a host, but an identifier that describes the URL scheme, much like a UTI identifier for a file type describes a specific file type. For example, you could use com.myCompany.myApp.url or something similar as the identifier.

You should be fine if you create a scheme of form x-myapp: and then use that as the redirect URL.

An example from a proposed Info.plist would be:

<dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLName</key>
    <string>com.myCompany.myApp.url</string>
    <key>CFBundleURLSchemes</key>
    <array>
        <string>x-myapp</string>
    </array>
</dict>

The CFBundleURLName corresponds in the Xcode GUI to URL Identifier.

Upvotes: 2

Related Questions