Good morning,
I'm trying to create the login and register process in my first iOS app and I'm a little bit lost. I tried to search a lot of post regarding the redirect but it's never working in my app and I really need your help with that.
That's my Storyboard:
And I would like to redirect the user to the 'Main' when the user is logged in correctly and when the user is registered correctly. I will be much appreciated if you can help me with this problem because I'm really stuck with this problem.
I will also add the code from my ViewController.m (AupViewController.m) if that helps you:
#import "aupViewController.h"
#import <FacebookSDK/FacebookSDK.h>
#import "SBJson.h"
@implementation MainViewController
@end
@implementation aupViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.loginButton.readPermissions = @[@"public_profile", @"email"];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Default.png"]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end
@implementation LoginViewController
-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
- (IBAction)backgroundClick:(id)sender
{
[txtPassword resignFirstResponder];
[txtUsername resignFirstResponder];
[_txtMail resignFirstResponder];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([txtPassword isFirstResponder] && [touch view] != txtPassword) {
[txtPassword resignFirstResponder];
}
if ([txtUsername isFirstResponder] && [touch view] != txtUsername) {
[txtUsername resignFirstResponder];
}
if ([_txtMail isFirstResponder] && [touch view] != _txtMail) {
[_txtMail resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
@synthesize txtUsername;
@synthesize txtPassword;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Default.png"]];
}
- (void)viewDidUnload
{
[self setTxtUsername:nil];
[self setTxtPassword:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (void) alertStatus:(NSString *)msg :(NSString *) title
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}
- (IBAction)registerClicked:(id)sender {
@try {
if([[txtUsername text] isEqualToString:@""] || [[txtPassword text] isEqualToString:@""] || [[_txtMail text] isEqualToString:@""]) {
[self alertStatus:@"Porfavor, introduce el usuario y contraseña" :@"Error"];
} else {
NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@&mail=%@",[txtUsername text],[txtPassword text],[_txtMail text]];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"http://autograpp.com/register.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [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: %d", [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(@"%d",success);
if(success == 1)
{
NSLog(@"Login SUCCESS");
[self alertStatus:@"Bienvenido a Autograpp." :@"Datos correctos"];
} 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"];
}
}
- (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://autograpp.com/login.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [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: %d", [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(@"%d",success);
if(success == 1)
{
NSLog(@"Login SUCCESS");
[self alertStatus:@"Bienvenido a Autograpp." :@"Datos correctos"];
} 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"];
}
}
@end
If you see anything wrong, please tell me, because that's my first app and I'm trying to learn.
Thanks in advance.
