ios - Redirect View login using xcode -
good morning,
i'm trying create login , register process in first ios app , i'm little bit lost. tried search lot of post regarding redirect it's never working in app , need that.
that's storyboard:
and redirect user 'main' when user logged in correctly , when user registered correctly. appreciated if can me problem because i'm stuck problem.
i add code viewcontroller.m (aupviewcontroller.m) if 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 cached data, images, etc aren't in use. } #pragma mark - view lifecycle - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. //self.view.backgroundcolor = [uicolor colorwithpatternimage:[uiimage imagenamed:@"default.png"]]; } - (void)viewdidunload { [self settxtusername:nil]; [self settxtpassword:nil]; [super viewdidunload]; // release retained subviews of 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 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 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 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 see wrong, please tell me, because that's first app , i'm trying learn.
thanks in advance.
your coding fine, u need implement navigation controller in front of root controller,
and redirect process use segue
method particular view controller, fine use line
[self performseguewithidentifier:@"identifiername" sender:self];
more ref :more reference here
Comments
Post a Comment