[英]How to integrate Linkedin SDK in app to Login & share using Swift
I am trying to integrate LinkedIn SDK in iOS using swift 我正在尝试使用swift在iOS中集成LinkedIn SDK
I found the below code in objective-C 我在Objective-C中找到了以下代码
I am very new to swift, I tried to convert this code in swift but it doesn't work. 我是swift的新手,我试图在swift中转换这个代码,但它不起作用。 Plz suggest me How can I convert this below code in swift.
Plz建议我如何在swift中转换下面的代码。 or how can I integrate Linkedin Sdk for login & share through my app using swift..
或者我如何整合Linkedin Sdk登录并使用swift通过我的应用程序分享..
enter code here
[LISDKSessionManager createSessionWithAuth:permissions state:nil showGoToAppStoreDialog:YES successBlock:^(NSString *returnState){
NSLog(@"%s","success called!");
LISDKSession *session = [[LISDKSessionManager sharedInstance] session];
NSLog(@"Session : %@", session.description);
[[LISDKAPIHelper sharedInstance] getRequest:@"https://api.linkedin.com/v1/people/~"
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 valueForKey: @"lastName"]];
NSLog(@"Authenticated user name : %@", authUsername);
[self.lblAuthenticatedUser setText: authUsername];
} error:^(LISDKAPIError *apiError) {
NSLog(@"Error : %@", apiError);
}];
} errorBlock:^(NSError *error) {
NSLog(@"Error called : %@", error);
}];
This is how I managed to authenticate a user via LinkedIn, using SwiftyJSON library to parse the response. 这就是我设法通过LinkedIn验证用户的方法,使用SwiftyJSON库来解析响应。 https://github.com/SwiftyJSON/SwiftyJSON As of May 2015 LinkedIn limited access to their API allowing only basic profile fields/email to be accessed.
https://github.com/SwiftyJSON/SwiftyJSON截至2015年5月,LinkedIn限制访问其API,只允许访问基本配置文件字段/电子邮件。 You also need to set the basic permissions for r_basicprofile and r_emailaddress under your app in the developer console in order for this to work.
您还需要在开发人员控制台中的应用程序下为r_basicprofile和r_emailaddress设置基本权限,以使其正常工作。
Hope this helps 希望这可以帮助
@IBAction func connectWithLinkedIn(sender: AnyObject) {
let url = NSString(string:"https://api.linkedin.com/v1/people/~:(id,industry,firstName,lastName,emailAddress,headline,summary,publicProfileUrl,specialties,positions:(id,title,summary,start-date,end-date,is-current,company:(id,name,type,size,industry,ticker)),pictureUrls::(original),location:(name))?format=json")
let permissions: [AnyObject] = [LISDK_BASIC_PROFILE_PERMISSION, LISDK_EMAILADDRESS_PERMISSION]
LISDKSessionManager.createSessionWithAuth(permissions, state: nil, showGoToAppStoreDialog: true, successBlock: { (success) -> () in
if LISDKSessionManager.hasValidSession() {
LISDKAPIHelper.sharedInstance().getRequest(url as String, success: {
response in
print(response)
print("successfully signed in")
dispatch_async(dispatch_get_main_queue(), { () -> () in
if let dataFromString = response.data.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
let result = JSON(data: dataFromString)
LISDKSessionManager.clearSession()
//Do something with the response for example
var picURL: String!
for stringInArray in result["pictureUrls"]["values"]{
let value = stringInArray.1.stringValue
print(value)
picURL = value
}
print(result["pictureUrls"]["values"].arrayValue)
print(result["specialties"].stringValue)
}
})
}, error: {
error in
LISDKAPIHelper.sharedInstance().cancelCalls()
LISDKSessionManager.clearSession()
print(error.localizedDescription)
//Do something with the error
})
}
print("success called!")
}, errorBlock: { (error) -> () in
print("%s", "error called!")
LISDKAPIHelper.sharedInstance().cancelCalls()
LISDKSessionManager.clearSession()
})
}
You can prefer this snippet for LinkedIn sharing: 你可以更喜欢这个片段用于LinkedIn共享:
Paste it into the valid session condition LISDKSessionManager.hasValidSession()
将其粘贴到有效的会话条件
LISDKSessionManager.hasValidSession()
Swift 3x : Swift 3x :
let url: String = "https://api.linkedin.com/v1/people/~/shares"
let payloadStr: String = "{\"comment\":\"YOUR_APP_LINK_OR_WHATEVER_YOU_WANT_TO_SHARE\",\"visibility\":{\"code\":\"anyone\"}}"
let payloadData = payloadStr.data(using: String.Encoding.utf8)
LISDKAPIHelper.sharedInstance().postRequest(url, body: payloadData, success: { (response) in
print(response!.data)
}, error: { (error) in
print(error as! Error)
let alert = UIAlertController(title: "Alert!", message: "aomething went wrong", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.