I have three different UITextField
in a "ViewController" and I would like to change these values for the ones I have in my MySQL Database. (I would like to show: followers, pictures and posts).
I know MySQL and PHP, but I'm stuck at the moment because I don't know how to do it.
First I would like to say that at the beginning, the user is going to log in using a form PHP+MySQL, maybe I have to store the username in some "var" and user later for populate those three labels with the information.
The problem is that this is my first app, and I don't know how to do it, that's why I need your help. How can I put the values from MySQL to XCode, and also, how can I store the username in a "global var" in order to use it later?
If you know any better process, please tell me, because I really want to know how to do this, because later I will have to show the profile image, profile name, etc, etc.
Edited with code:
ViewController.m
That's my login button, I will add the "load" inside the login.php file.
- (IBAction)loginClicked:(id)sender {
@try {
if([[_txtUsername text] isEqualToString:@""] || [[_txtPassword text] isEqualToString:@""] ) {
[self alertStatus:@"Porfavor, introduce el usuario y contraseña" :@"Error"];
} else {
NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[_txtUsername text],[_txtPassword text]];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"http://example.com/login.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"%@",jsonData);
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
NSLog(@"%ld",(long)success);
if(success == 1)
{
NSLog(@"Login SUCCESS");
//[self alertStatus:@"Welcome." :@"Datos correctos"];
[self performSegueWithIdentifier:@"Main" sender:self];
} else {
NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
[self alertStatus:error_msg :@"Datos incorrectos"];
}
} else {
if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Ha ocurrido un problema inesperado" :@"Error"];
}
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Error." :@"Error"];
}
}
Thanks in advance.