当用户登录应用程序时,您应该通过 LinkedIn API 请求访问令牌。之后,您使用 NSUserDefaults 保存此访问令牌。因此,当用户打开应用程序时,您会检查 accesstoken 是否存在。如果 accesstoken 存在,则用户已经登录到应用程序。
- (void)getAccessToken:(NSString *)authorizationCode success:(void (^)(NSDictionary *))success failure:(void (^)(NSError *))failure
{
NSString *accessTokenUrl = @"/uas/oauth2/accessToken?grant_type=authorization_code&code=%@&redirect_uri=%@&client_id=%@&client_secret=%@";
NSString *url = [NSString stringWithFormat:accessTokenUrl, authorizationCode, [self.application.redirectURL LIAEncode], self.application.clientId, self.application.clientSecret];
[self POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *accessToken = [responseObject objectForKey:@"access_token"];
NSTimeInterval expiration = [[responseObject objectForKey:@"expires_in"] doubleValue];
// store credentials
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:accessToken forKey:LINKEDIN_TOKEN_KEY];
[userDefaults setDouble:expiration forKey:LINKEDIN_EXPIRATION_KEY];
[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:LINKEDIN_CREATION_KEY];
[userDefaults synchronize];
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}];
}