Friday, 15 April 2016

Linkdin Login in ios application

In one of my application i have to provide option that user can login though linkdn.

First of all make 

1) Add linkedin-sdk framework in your project

2) Get access token from this method:

[LISDKSessionManager createSessionWithAuth:[NSArray arrayWithObjects:LISDK_BASIC_PROFILE_PERMISSION, LISDK_EMAILADDRESS_PERMISSION, nil]
                                     state:@"some state"
                                     showGoToAppStoreDialog:YES
                                               successBlock:^(NSString *returnState) {

                                                   NSLog(@"%s","success called!");
                                                   LISDKSession *session = [[LISDKSessionManager sharedInstance] session];
                                                   NSLog(@"value=%@ isvalid=%@",[session value],[session isValid] ? @"YES" : @"NO");
                                                   NSMutableString *text = [[NSMutableString alloc] initWithString:[session.accessToken description]];
                                                   [text appendString:[NSString stringWithFormat:@",state=\"%@\"",returnState]];
                                                   NSLog(@"Response label text %@",text);
                                                   _responseLabel.text = text;
                                                   self.lastError = nil;
                                                   // retain cycle here?
                                                   [self updateControlsWithResponseLabel:NO];

                                               }
                                                 errorBlock:^(NSError *error) {
                                                     NSLog(@"%s %@","error called! ", [error description]);
                                                     self.lastError = error;
                                                     //  _responseLabel.text = [error description];
                                                     [self updateControlsWithResponseLabel:YES];
                                                 }
     ];
   
3) if you want id,first-name,last-name,maiden-name,email-address after authorization you can apply this code:

   [[LISDKAPIHelper sharedInstance] apiRequest:https://www.linkedin.com/v1/people/~:(id,first-name,last-name,maiden-name,email-address)
                                             method:method
                                               body:nil
                                            success:^(LISDKAPIResponse *response) {
                                                NSLog(@"success called %@", response.data);
                                                AppDelegate *myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                                                dispatch_sync(dispatch_get_main_queue(), ^{
                                                    [myAppDelegate showAPIResponsePage:method
                                                                                status:response.statusCode
                                                                               request:_resourceTextField.text
                                                                              response:response.data];
                                                });
                                            }
                                              error:^(LISDKAPIError *apiError) {
                                                  NSLog(@"error called %@", apiError.description);
                                                   AppDelegate *myAppDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
                                                  dispatch_sync(dispatch_get_main_queue(), ^{
                                                      LISDKAPIResponse *response = [apiError errorResponse];
                                                      NSString *errorText;
                                                      if (response) {
                                                          errorText = response.data;
                                                      }
                                                      else {
                                                          errorText = apiError.description;
                                                      }
                                                  [myAppDelegate showAPIResponsePage:[self getMethod]
                                                                              status:[apiError errorResponse].statusCode
                                                                             request:_resourceTextField.text
                                                                            response:errorText];
                                                  });
                                              }];

 
you can check your method output by this link:

https://apigee.com/console/linkedin
 

No comments:

Post a Comment