![](/img/trans.png)
[英]share content on linkedIn without using OAMutableURLRequest in iOS using objective-c?
[英]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.