问题
I am trying to integrate LinkedIn SDK in iOS using swift
I found the below code in objective-C (https://developer.linkedin.com/docs/signin-with-linkedin)
NSString *url = [NSString initWithString:@"https://api.linkedin.com/v1/people/~"];
if ([LISDKSessionManager hasValidSession]) {
[[LISDKAPIHelper sharedInstance] getRequest:url
success:^(LISDKAPIResponse *response) {
// do something with response
}
error:^(LISDKAPIError *apiError) {
// do something with error
}];
]}
How to convert this to swift.
I am very new to swift
回答1:
var url = NSString(string:"https://api.linkedin.com/v1/people/~")
if LISDKSessionManager.hasValidSession {
LISDKAPIHelper.sharedInstance().getRequest(url, success: {
response in
//Do something with the response
}, error: {
error in
//Do something with the error
})
}
This (I think its correct) is the translated version. I don't know Objective-C, I just used my knowledge of Swift to try and figure this out.
Have you learned about closures yet? If not, I don't recommend using SDKs like the LinkedIn one because they rely on closures for many networking requests. I'd check out Treehouse Inc., a coding course site which offers great courses on closures in Swift (along with a bunch of other stuff).
回答2:
var url = "https://api.linkedin.com/v1/people/~"
if LISDKSessionManager.hasValidSession()
{
try? LISDKAPIHelper.sharedInstance().getRequest(url, success: {(_ response: LISDKAPIResponse) -> Void in
// do something with response
})
}
this is in swift 4
来源:https://stackoverflow.com/questions/31318649/linkedin-sdk-ios-swift