iphone - linkedin Oauth如何记住登录

标签 iphone ios objective-c oauth linkedin

几周前我开始从事 iOS 开发工作,最近我一直在尝试将社交网络集成到应用程序(Facebook、Twitter、LinkedIn)中。我设法完成了 Facebook 和 Twitter 登录集成,但我被困在了 LinkedIn。

使用此示例进行 LinkedIn 集成 https://github.com/synedra/LinkedIn-OAuth-Sample-Client ,我可以登录领英,但一旦我的应用关闭,我必须重新登录。

为了让应用程序记住登录的用户,我应该怎么做?我应该如何使用登录时的访问 token 在我的应用程序的其他 View 上实现从 LinkedIn(社交、网络等)获取提要?

最佳答案

当用户登录应用程序时,您应该通过 LinkedIn API 请求访问 token 。之后,您使用 NSUserDefaults 保存此访问 token 。因此,当用户打开应用程序时,您会检查 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);
   }];
}

关于iphone - linkedin Oauth如何记住登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17074944/

相关文章:

ios - UITextView textViewDidChange : does not recognize newline character

objective-c - 如何实现基于属性(property)值(value)的平等?

objective-c - 如何删除 Mac 应用程序的首选项?

iphone - -(void)textFieldDidBeginEditing :(UITextField *)sender. 。当我选择文本字段时不会调用此函数?

iOS - 无法定位异常的位置

iphone - HmacSHA256 objective-c 加密

iphone - 如何在 iPhone 应用程序中使用动画 .gif?

iphone - 在 iPhone 应用程序中编辑 PDF

ios - 如何减少 collectionview 中两个单元格之间的垂直空间?

ios - 简单的 iOS 应用程序,用于在 UIWebView *使用*后退/刷新 Controller 中显示我的网站