Я разрабатываю приложение со страницей входа. Когда приложение запускается, отображается экран входа в систему, и вы не можете получить доступ к приложению до тех пор, пока не будете подключены. Чтобы подключиться к приложению, вы вводите свое имя пользователя и пароль. Когда вы нажимаете кнопку «connect», json-данные, содержащие имя пользователя и пароль, отправляются в веб-службу, которые проверяют наличие учетных данных. Если они существуют, сервер отправляет json-файл, содержащий «существует»: «true»обработчик входа и завершения
Проблема в том, что код, проверяющий этот Json-файл, находится в завершенииHandler моего NSURLSession, а метод возвращает «NO» перед данными Json , поэтому я не могу подключиться к моему приложению. Как это трудно объяснить, вот мой код:
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");
}
Я надеюсь, что вы меня поняли, спасибо за вашу помощь!
Simon