【问题标题】:LinkedIn integration to objective-c iOS, Without installing LinkedIn AppLinkedIn集成到objective-c iOS,无需安装LinkedIn App
【发布时间】:2018-12-25 23:47:57
【问题描述】:

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

        }];

【问题讨论】:

  • 你应该可以使用canOpenURLlinkedin:// url

标签: ios objective-c linkedin linkedin-api


【解决方案1】:

第 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 步:在您希望linkedIn 登录的类中创建一个属性:

@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”。

就是这样。快乐编码。 (如果有任何问题,请在提交中问我)。

【讨论】:

  • 嗨,谢谢您的回复,我只是像您一样尝试,但我收到 AFNetworking/AFHTTPRequestOperationManager.h' 文件未找到错误,因为我使用更新的 AFNetworking pod 文件,所以我收到此错误。
  • 是的。不能用旧版本吗?在其他代码文件中使用 2.5.4 有什么问题吗?
  • 是的,我已经使用最新的 AFNetworking 开发了漏洞项目,所以现在我无法修改总代码,还有其他开发选项。如果我使用的是 2.5.4,那么我会得到更多更改。
  • 哦,那么您必须使用此答案修改 LinkedIn SDK。 stackoverflow.com/questions/33893706/…
猜你喜欢
  • 2017-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-26
  • 2015-09-27
  • 2011-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多