我正在开发一个登录页面的应用程序。当应用程序启动时,将显示登录屏幕,并且只有连接后才能访问该应用程序。要连接到应用程序,请输入您的用户名和密码。当您按下“连接”按钮,包含用户名和密码JSON数据被发送到Web服务,它检查是否存在的凭据。如果它们存在,服务器发送一个包含“exists”的json文件:“true”登录并完成处理
问题是检查此Json文件的代码位于NSURLSession的completionHandler中,并且该方法在Json数据之前返回“NO”被检查,所以我无法连接到我的应用程序。由于这很难解释,这里是我的代码:
GSBconnexion.m:
#import "GSBconnexion.h"
@implementation GSBconnexion
-(bool)logConnexionWithUserName:(NSString *)username
password:(NSString *)password{
__block BOOL allowConnexion;
NSDictionary *connexion = @{
@"username": username,
@"password": password,
@"target": @"app"
};
NSError *error;
NSData *jsonLogData = [NSJSONSerialization dataWithJSONObject:connexion options:NSJSONWritingPrettyPrinted
error:&error];
if (! jsonLogData) {
NSLog(@"Got an error: %@", error);
}
NSData *logData = jsonLogData;
NSString *testString = [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding];
NSString *logLength = [NSString stringWithFormat:@"%lu", (unsigned long)[testString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://192.168.5.133:1337/login"]];
[request setHTTPMethod:@"POST"];
[request setValue:logLength forHTTPHeaderField:@"Content-lenght"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:logData];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
NSDictionary *serverResponse = [NSJSONSerialization JSONObjectWithData:data options:
NSJSONReadingMutableContainers error:&error];
int canIConnect = [serverResponse[@"exist"] intValue];
NSLog(@"%d",canIConnect);
if (canIConnect == 1) {
NSLog(@"OKKK");
allowConnexion = YES;
NSString *sessionID = [[NSString alloc]initWithString:serverResponse[@"_id"]];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:sessionID forKey:@"SessionID"];
[userDefaults synchronize];
NSLog(@"ID Session:%@",[userDefaults objectForKey:@"sessionID"]);
}
else {
allowConnexion=NO;
}
}] resume];
NSLog(@"JSON envoyé: \n\n%@",testString);
return allowConnexion;
}
@end
GSBLoginController:
- (IBAction)connect:(id)sender {
connectButton.hidden = YES;
loading.hidden = NO;
UIViewController* homePage = [self.storyboard instantiateViewControllerWithIdentifier:@"homePage"];
GSBconnexion *login = [[GSBconnexion alloc]init];
NSString *username = [[NSString alloc]initWithFormat:@"%@",usernameTextField.text];
NSString *password = [[NSString alloc]initWithFormat:@"%@",pwdTextField.text];
BOOL authorized = [login logConnexionWithUserName:username password:password];
if (authorized) {
[self presentViewController:homePage animated:YES completion:nil];
}
else {
connectButton.hidden = NO;
loading.hidden=YES;
[email protected]"";
[email protected]"";
errorLabel.text = @"Connexion impossible, merci de réessayer.\nSi le problème persiste, veuillez contacter un administrateur.";
}
NSLog(authorized ? @"Yes" : @"No");
}
我希望你明白我的意思,感谢您的帮助!
西蒙