领英集成到objective-c iOS,无需安装领英应用程序



将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、客户端机密和重定向URL(。

- (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"。

就是这样。编码快乐。(如果出现任何问题,请在提交时询问我(

相关内容

最新更新