簡體   English   中英

將LinkedIn集成到Objective-C iOS,無需安裝LinkedIn App

[英]LinkedIn integration to objective-c iOS, Without installing LinkedIn App

將LinkedIn應用程序集成到我的iOS(Objective-c)項目中,在安裝LinkedIn時運行良好,否則顯示一個警報安裝LinkedIn應用程序,但是現在我想處理是否沒有安裝LinkedIn應用程序,如果有任何想法請給我答案,請檢查下面的代碼以了解。

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);

        }];

步驟1:在您的Pod文件中添加這兩個Pod。

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

步驟2:導入這些庫

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

步驟3:在您要鏈接的類中創建屬性,請登錄:

@property (nonatomic, strong) LIALinkedInHttpClient *client;

步驟4:在您要使用的類中編寫此方法,並提供鏈接應用程序的常量值(例如clientID和client Secret和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];
}

步驟5:在要在viewDidLoad方法中使用的類中編寫此行。

 _client = [self clientSettings];

步驟6:在Appdelegate的文件方法“ 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;
}

步驟7:現在在您要在登錄中使用鏈接的類中編寫這些方法。

- (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);
    }];

}

步驟8:現在僅在IBAction按鈕上調用方法“ didTapConnectWithLinkedIn”。

而已。 快樂的編碼。 (如果有任何問題,請在提交時詢問我)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM