I am trying to integrate LinkedIn SDK in iOS using swift
I found the below code in objective-C
I am very new to swift, I tried to convert this code in swift but it doesn't work. Plz suggest me How can I convert this below code in swift. or how can I integrate Linkedin Sdk for login & share through my app using 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. https://github.com/SwiftyJSON/SwiftyJSON As of May 2015 LinkedIn limited access to their API allowing only basic profile fields/email to be accessed. 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.
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:
Paste it into the valid session condition LISDKSessionManager.hasValidSession()
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)
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.