Today we will learn how to integrate Linkedin SignIn in our app and how to fetch users data.

Steps:

  1. You need a linked account for it. Sign in to Linkedin Developer Account.
  2. Create a new Xcode Project (LetsSay LinkedinDemo).
  3. Go to Linkedin Developer Account Linkedin and Click on Create Application

Screen Shot 2016-07-15 at 11.57.23 PM

4. Fill the details and click on Submit Button.

Screen Shot 2016-07-16 at 12.03.27 AM

5. Now click on Mobile Section and copy the AppId and give Your Bundle id here.

Screen Shot 2016-07-16 at 12.06.47 AM

6. now go to info List of Your app and write this AppiD(Replace YourAppId with the real Id) there.

Screen Shot 2016-07-16 at 12.14.21 AM.png

7. Now go to this Link and download Linkdin Sdks.

Screen Shot 2016-07-16 at 12.16.14 AM.png

8. Unzip the file and copy the Linkdin Sdk to Your Project Folder and then Add it. Click on Add other, it will take you to Your Project Folder then Add it.

Screen Shot 2016-07-16 at 12.19.04 AM.png

9. Now go to AppDelegate.m and add this Code

#import “AppDelegate.h”

#import <linkedin-sdk/LISDK.h>

@interface AppDelegate ()

@end

@implementation AppDelegate

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    return YES;

}

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

{

    if ([LISDKCallbackHandler shouldHandleUrl:url])

    {

        return [LISDKCallbackHandler application:application openURL:url sourceApplication:sourceApplication annotation:annotation];

    }

    return YES;

}

10. Now  go to storyBoard. Take two Buttons (Signin and Signet) , one imageView and three textfields. Make Proper Connection.

Screen Shot 2016-07-16 at 12.24.44 AM

11. Your ViewController.h will look something like this.

#import <UIKit/UIKit.h>

#import <linkedin-sdk/LISDK.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *firstnameTextField;

@property (weak, nonatomic) IBOutlet UITextField *lastnameTextField;

@property (weak, nonatomic) IBOutlet UITextField *emailTextField;

@property (weak, nonatomic) IBOutlet UIImageView *profileImage;

12.  Go to ViewController.m and write the Button Action for signing and signout.

– (IBAction)linkedinSignInButtonAction:(id)sender {

NSArray *permissions = [NSArray arrayWithObjects:LISDK_BASIC_PROFILE_PERMISSION, LISDK_EMAILADDRESS_PERMISSION, nil];

 

    [LISDKSessionManager createSessionWithAuth:permissions state:nil showGoToAppStoreDialog:YES successBlock:^(NSString *returnState){

        NSLog(@”%s”,”success called!”);

        LISDKSession *session = [[LISDKSessionManager sharedInstance] session];

        NSLog(@”Session  : %@”, session.description);

 

        [[LISDKAPIHelper sharedInstance] getRequest:@”https://api.linkedin.com/v1/people/~:(picture-url)?format=json”

                                            success:^(LISDKAPIResponse *response) {

 

                                                NSData* data = [response.data dataUsingEncoding:NSUTF8StringEncoding];

                                                NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

 

                                                NSString *urlString = [dictResponse valueForKey: @”pictureUrl”];

                                                NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: urlString]];

 

                                                dispatch_async(dispatch_get_main_queue(), ^{

                                                    // code here

                                                     self.profileImage.image = [UIImage imageWithData: imageData];                                               });

 

 

                                            } error:^(LISDKAPIError *apiError) {

                                                NSLog(@”Error  : %@”, apiError);

                                            }];

 

 

        [[LISDKAPIHelper sharedInstance] getRequest:@”https://api.linkedin.com/v1/people/~&#8221;

                                            success:^(LISDKAPIResponse *response) {

 

                                                NSData* data = [response.data dataUsingEncoding:NSUTF8StringEncoding];

                                                NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

 

                                                NSString *authUsername = [NSString stringWithFormat: @”%@ %@”, [dictResponse valueForKey: @”firstName”], [dictResponse valueForKey: @”lastName”]];

                                                NSLog(@”Authenticated user name  : %@”, authUsername);

 

                                                dispatch_async(dispatch_get_main_queue(), ^{

                                                    // code here

                                                    self.firstnameTextField.text = [dictResponse valueForKey: @”firstName”];

                                                    self.lastnameTextField.text = [dictResponse valueForKey: @”lastName”];                                                });

 

 

                                            } error:^(LISDKAPIError *apiError) {

                                                NSLog(@”Error  : %@”, apiError);

                                            }];

 

 

        [[LISDKAPIHelper sharedInstance] getRequest:@”https://api.linkedin.com/v1/people/~:(email-address)?format=json”

                                            success:^(LISDKAPIResponse *response) {

 

                                                NSData* data = [response.data dataUsingEncoding:NSUTF8StringEncoding];

                                                NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

 

                                                dispatch_async(dispatch_get_main_queue(), ^{

                                                self.emailTextField.text = [dictResponse valueForKey: @”emailAddress”];

                                                });

 

 

                                            } error:^(LISDKAPIError *apiError) {

                                                NSLog(@”Error  : %@”, apiError);

                                            }];

 

 

    } errorBlock:^(NSError *error) {

        NSLog(@”Error called  : %@”, error);

    }];

 

 

}

– (IBAction)signOutButttonAction:(id)sender {

    [LISDKSessionManager clearSession];

}

13. Run the code on your Device. if linked is installed and signing then it will fetch the data, otherwise it will ask user to download it.

Enjoy Coding 🙂

Advertisements