我的iOS应用程序中有一个SSKeychain的凭证存储库的奇怪错误。当用户登录时,我将大量信息存储在凭证存储中。但最近凭据存储没有设置项目。
奇怪的是,它不会一直发生。有时它会设置信息并按预期工作。有时,凭据存储值在登录后返回nil
。我的代码如下所示:
- (void)login:(id)sender {
[SVProgressHUD show];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setResponseSerializer:[LoginResponseSerializer serializer]];
__weak typeof(self)weakSelf = self;
NSString *urlString = [NSString stringWithFormat:@"%s%s", kBaseURL, kLoginURL];
if (self.passwordField.text && self.emailField.text) {
NSDictionary *params = @{ @"email": self.emailField.text, @"password": self.passwordField.text};
self.sessionManager = manager;
[manager POST:urlString parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
// prgress implementation
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *user = [responseObject objectForKey:@"user"];
NSString *token = user[@"auth_token"];
NSString *avatarURL = user[@"avatar_url"];
NSString *userID = [NSString stringWithFormat: @"%@", user[@"id"]];
NSString *firstName = user[@"first_name"];
NSString *lastName = user[@"last_name"];
NSString *levelOfStudy = user[@"level"];
BOOL teecher = [user[@"teecher"] boolValue];
[weakSelf.credentialStore setTeecher:teecher];
[weakSelf.credentialStore setUserId:userID];
[weakSelf.credentialStore setAvatarURL:avatarURL];
[weakSelf.credentialStore setAuthToken:token];
[weakSelf.credentialStore setFirstName:firstName];
[weakSelf.credentialStore setLastName:lastName];
[weakSelf.credentialStore setLevelOfStudy:levelOfStudy];
[weakSelf.credentialStore setEmail:self.emailField.text];
[weakSelf.credentialStore setPassword:self.passwordField.text];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager.requestSerializer setValue:token forHTTPHeaderField:@"auth_token"];
[manager setResponseSerializer:[CustomerResponseSerializer serializer]];
NSString *urlString = [NSString stringWithFormat:@"%s%s", kBaseURL, kCustomerURL];
[manager GET:urlString parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
// progress implementation
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *sources = [responseObject objectForKey:@"sources"];
NSArray *cards = (NSArray *)sources[@"data"];
[weakSelf.credentialStore setHasCreditCard:(cards.count > 0)];
[weakSelf.credentialStore setSignUp:false];
[SVProgressHUD dismiss];
[self.navigationController popViewControllerAnimated:NO];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@"Failed to refresh cards."];
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@"Login Failed"];
}];
}
}
正如您在成功时所看到的那样,我将这些物品存放到凭证商店,但这是一个运气游戏,它在40%的时间内工作,而不是60%,这是令人困惑的。我也在努力在模拟器上重新创建问题。
如果有人有任何想法?或者需要查看更多代码让我知道。
答案 0 :(得分:1)
我遇到了类似的问题,当我添加Keychain Sharing权利时,它已得到解决。
有关如何启用Keychain Sharing的更多详细信息: https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/AppDistributionGuide/AddingCapabilities/AddingCapabilities.html#//apple_ref/doc/uid/TP40012582-CH26-SW15
答案 1 :(得分:0)
我认为您忘记了resume
您的网络电话。如果有效,请尝试如下,
- (void)login:(id)sender {
[SVProgressHUD show];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setResponseSerializer:[LoginResponseSerializer serializer]];
__weak typeof(self)weakSelf = self;
NSString *urlString = [NSString stringWithFormat:@"%s%s", kBaseURL, kLoginURL];
if (self.passwordField.text && self.emailField.text) {
NSDictionary *params = @{ @"email": self.emailField.text, @"password": self.passwordField.text};
self.sessionManager = manager;
[[manager POST:urlString parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
// prgress implementation
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *user = [responseObject objectForKey:@"user"];
NSString *token = user[@"auth_token"];
NSString *avatarURL = user[@"avatar_url"];
NSString *userID = [NSString stringWithFormat: @"%@", user[@"id"]];
NSString *firstName = user[@"first_name"];
NSString *lastName = user[@"last_name"];
NSString *levelOfStudy = user[@"level"];
BOOL teecher = [user[@"teecher"] boolValue];
[weakSelf.credentialStore setTeecher:teecher];
[weakSelf.credentialStore setUserId:userID];
[weakSelf.credentialStore setAvatarURL:avatarURL];
[weakSelf.credentialStore setAuthToken:token];
[weakSelf.credentialStore setFirstName:firstName];
[weakSelf.credentialStore setLastName:lastName];
[weakSelf.credentialStore setLevelOfStudy:levelOfStudy];
[weakSelf.credentialStore setEmail:self.emailField.text];
[weakSelf.credentialStore setPassword:self.passwordField.text];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager.requestSerializer setValue:token forHTTPHeaderField:@"auth_token"];
[manager setResponseSerializer:[CustomerResponseSerializer serializer]];
NSString *urlString = [NSString stringWithFormat:@"%s%s", kBaseURL, kCustomerURL];
[[manager GET:urlString parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
// progress implementation
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *sources = [responseObject objectForKey:@"sources"];
NSArray *cards = (NSArray *)sources[@"data"];
[weakSelf.credentialStore setHasCreditCard:(cards.count > 0)];
[weakSelf.credentialStore setSignUp:false];
[SVProgressHUD dismiss];
[self.navigationController popViewControllerAnimated:NO];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@"Failed to refresh cards."];
}] resume];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@"Login Failed"];
}] resume];
}
}