简体   繁体   中英

LinkedIn integration to objective-c iOS, Without installing LinkedIn App

Integrate LinkedIn application into my iOS (Objective-c) project it is working good when LinkedIn is installed otherwise its showing one alert install LinkedIn app But now I want to handle if LinkedIn app is not installed if any body have idea give me the answer and please check below code for understanding .

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];
[[LISDKAPIHelper sharedInstance] getRequest:[NSString stringWithFormat:@"%@/people/~:(id,first-name,last-name,maiden-name,email-address,picture-url)", LINKEDIN_API_URL]

                                                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];
} error:^(LISDKAPIError *apiError) {

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

             }];

        } errorBlock:^(NSError *error) {

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

        }];

Step 1: add these two pods in your pod file.

pod 'AFNetworking', '~> 2.5.4'
pod 'IOSLinkedInAPI', '~> 2.0'

Step 2: Import these libraries

#import <linkedin-sdk/LISDK.h>
#import <LIALinkedInHttpClient.h>
#import <LIALinkedInApplication.h>
#import <AFHTTPRequestOperation.h>

Step 3: Make a property in the class where you want linkedIn login:

@property (nonatomic, strong) LIALinkedInHttpClient *client;

Step 4: Write this method in the class where you want to use and also give the constant values of your linked app(like clientID and client Secret and redirectURL).

- (LIALinkedInHttpClient *)clientSettings {
    LIALinkedInApplication *application = [LIALinkedInApplication     applicationWithRedirectURL:kRedirectURL     clientId:kClientID clientSecret:kClientSecret   state:@"DCEEFWF45453sdffef424342" grantedAccess:@[@"r_basicprofile",@"r_emailaddress"]];

    return [LIALinkedInHttpClient clientForApplication:application         presentingViewController:self.window.rootViewController];
}

Step 5: Write this line in the class where you want to use in viewDidLoad method.

 _client = [self clientSettings];

Step 6: Write this line in Appdelegate's file's method "openURL".

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

    BOOL handled;

    if ([LISDKCallbackHandler shouldHandleUrl:url]) {
        handled = [LISDKCallbackHandler application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
    }
    return handled;
}

Step 7: Now write these methods in the class where you want to use linked in login.

- (void)didTapConnectWithLinkedIn {

    [self.client getAuthorizationCode:^(NSString *code) {

        [self.client getAccessToken:code success:^(NSDictionary *accessTokenData) {
            NSString *accessToken = [accessTokenData objectForKey:@"access_token"];
            [self requestMeWithToken:accessToken];
        }                   failure:^(NSError *error) {
            NSLog(@"Querying accessToken failed %@", error);
        }];
    }                      cancel:^{
        NSLog(@"Authorization was cancelled by user");
    }                     failure:^(NSError *error) {

        NSLog(@"Authorization failed %@", error);
    }];
}

- (void)requestMeWithToken:(NSString *)accessToken {

    [self.client GET:[NSString stringWithFormat:@"https://api.linkedin.com/v1/people/~:(id,first-name,last-name,maiden-name,email-address,formatted-name,phonetic-last-name,location:(country:(code)),industry,distance,current-status,current-share,network,skills,phone-numbers,date-of-birth,main-address,positions:(title),educations:(school-name,field-of-study,start-date,end-date,degree,activities))?oauth2_access_token=%@&format=json", accessToken] parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *result) {


        NSLog(@"current user %@", result);


    }        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"failed to fetch current user %@", error);
    }];

}

Step 8: Now call only the method "didTapConnectWithLinkedIn" on your IBAction button.

Thats it. Happy coding. (if any problem comes please ask me in commits).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM