Llamada a webservice (ws)
if ([self testInternetConnection]) {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[PreferenceUtils setEmail:self.textFieldEmail.text];
[PreferenceUtils setPassword:self.textFieldPassword.text];
//LLEVAR A BACKGROUND PARA HACER LA LLAMADA DE WS A SEGUNDO PLANO
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[LORestManager sharedInstance] createAdmin:self.textFieldEmail.text
password:self.textFieldPassword.text
returnHandler:^(int messageId, id json) {
//LLEVAR AL THREAD PRINCIPAL PARA HACER OPERACIONES DE PANTALLA
dispatch_async(dispatch_get_main_queue(), ^{
if(messageId>=0){
self.registered = YES;
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
[self performSegueWithIdentifier:@"showCreateFamily" sender:self];
} else {
switch (messageId) {
case -2:
//El email ya existe
[self showError:NSLocalizedString(@"Alert_error_-4", nil)];
break;
default:
//Error en la comunicación con el WS
[self showError:NSLocalizedString(@"error_server", nil)];
break;
}
}
});
}];
});
}
//En RestManager:
#pragma mark - Admin calls
-(void)createAdmin: (NSString *) user
password: (NSString*) password
returnHandler: (void (^)(int,id))handler{
@try {
NSString* url = [NSString stringWithFormat:@"%@/%@", RESTWSPATH, @"admins"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
NSMutableDictionary *admin = [[NSMutableDictionary alloc] init];
[admin setObject:user forKey:@"username"];
[admin setObject:password forKey:@"password"];
[params setObject:admin forKey:@"admin"];
[self postRequest:url withParams:params completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (!error && httpResp.statusCode == 201) {
if(data){
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
NSLog(@"ReceivedJSON: %@", json);
if (json && !error) {
NSNumber *errorCode = 0;
[PreferenceUtils setUserId:json[@"id"]];
handler([errorCode intValue],json);
return;
} else {
NSLog(@"Error processing ReceivedJSON: %@", error.localizedDescription);
}
}
} else {
if (!error) {
NSLog(@"Error getting ReceivedJSON, httpResponse: %@", httpResp);
} else {
NSLog(@"Error getting ReceivedJSON, error: %@", error.localizedDescription);
}
}
if (httpResp.statusCode == 202){
//Email ya existe
handler(-2,nil);
}else {
handler(-1,nil);
}
}];
}
@catch (NSException *e) {
NSLog(@"Error getting ReceivedJSON: %@", e.reason );
}
}