Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 登录失败仍然会转到下一个视图控制器_Objective C_Ios7_Login_Segue - Fatal编程技术网

Objective c 登录失败仍然会转到下一个视图控制器

Objective c 登录失败仍然会转到下一个视图控制器,objective-c,ios7,login,segue,Objective C,Ios7,Login,Segue,虽然我以前成功地做到了这一点,但这次我想不出来了。我有一个基本的登录屏幕作为第一个VC启动时加载。成功登录(来自数据库的适当用户名/密码组合)确实允许在没有任何警报的情况下进行访问。但是…任何不正确的组合(或者根本没有用户名/密码),都会导致我想要的alertView,但下一个VC还是会加载。我做错了什么?在发生故障时,不应该有任何中断,只有一个警报 LoginViewController.m @implementation LoginViewController - (IBAction)un

虽然我以前成功地做到了这一点,但这次我想不出来了。我有一个基本的登录屏幕作为第一个VC启动时加载。成功登录(来自数据库的适当用户名/密码组合)确实允许在没有任何警报的情况下进行访问。但是…任何不正确的组合(或者根本没有用户名/密码),都会导致我想要的alertView,但下一个VC还是会加载。我做错了什么?在发生故障时,不应该有任何中断,只有一个警报

LoginViewController.m

@implementation LoginViewController

- (IBAction)unwindToLoginViewController:(UIStoryboardSegue *)unwindSegue
{
}

- (IBAction)loginClicked:(id)sender {
    NSInteger success = 0;
    @try {

        if([[self.usernameText text] isEqualToString:@""] || [[self.passwordText text] isEqualToString:@""] ) {

            [self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0];

        } else {
            NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[self.usernameText text],[self.passwordText text]];
            NSLog(@"PostData: %@",post);

            NSURL *url=[NSURL URLWithString:@"http://www.techinworship.com/auth.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);

                NSError *error = nil;
                NSDictionary *jsonData = [NSJSONSerialization
                                          JSONObjectWithData:urlData
                                          options:NSJSONReadingMutableContainers
                                          error:&error];

                success = [jsonData[@"success"] integerValue];
                NSLog(@"Success: %ld",(long)success);

                if(success == 1)
                {
                    NSLog(@"Login SUCCESS");
                } else {

                    NSString *error_msg = (NSString *) jsonData[@"error_message"];
                    [self alertStatus:error_msg :@"Sign in Failed!" :0];
                }

            } else {
                //if (error) NSLog(@"Error: %@", error);
                [self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0];
            }
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
        [self alertStatus:@"Sign in Failed." :@"Error!" :0];
    }
    if (success) {
        [self performSegueWithIdentifier:@"login_success" sender:self];
    }
}

- (void) alertStatus:(NSString *)msg :(NSString *)title :(int) tag
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:msg
                                                       delegate:self
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil, nil];
    alertView.tag = tag;
    [alertView show];
}
- (IBAction)backgroundTap:(id)sender {
    [self.view endEditing:YES];
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
@implementation LoginViewController
-(iAction)UnwindToLoginView控制器:(UIStoryboardSegue*)unwindSegue
{
}
-(iAction)登录勾选:(id)发件人{
NSInteger成功率=0;
@试一试{
if([[self.usernameText]IseQualtString:@”“][[self.passwordText]IseQualtString:@”“])){
[自我提醒状态:@“请输入电子邮件和密码”:@“登录失败!”:0];
}否则{
NSString*post=[[NSString alloc]initWithFormat:@“用户名=%@&密码=%@”,[self.usernameText],[self.passwordText];
NSLog(@“PostData:%@”,post);
NSURL*url=[NSURL URLWithString:@”http://www.techinworship.com/auth.php"];
NSData*postData=[post数据使用编码:NSASCIIStringEncoding allowLossyConversion:是];
NSString*postLength=[NSString stringWithFormat:@“%lu”,(无符号长)[postData长度]];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init];
[请求设置url:url];
[请求设置HttpMethod:@“POST”];
[请求设置值:HttpHeaderField的postLength:@“内容长度”];
[请求设置值:@“应用程序/json”用于HttpHeaderField:@“接受”];
[请求设置值:@“应用程序/x-www-form-urlencoded”forHTTPHeaderField:@“内容类型”];
[请求setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHttpSCCertificate:YES for主机:[url主机]];
NSError*error=[[NSError alloc]init];
NSHTTPURLResponse*响应=nil;
NSData*urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&响应错误:&错误];
NSLog(@“响应代码:%ld”,(长)[响应状态代码]);
如果([response statusCode]>=200&&[response statusCode]<300)
{
NSString*responseData=[[NSString alloc]initWithData:urlData编码:NSUTF8StringEncoding];
NSLog(@“响应==>%@”,响应数据);
n错误*错误=nil;
NSDictionary*jsonData=[NSJSONSerialization
JSONObjectWithData:urlData
选项:NSJSONReadingMutableContainers
错误:&错误];
success=[jsonData[@“success”]integerValue];
NSLog(@“成功:%ld”,(长)成功);
如果(成功==1)
{
NSLog(@“登录成功”);
}否则{
NSString*错误消息=(NSString*)jsonData[@“错误消息”];
[自我提醒状态:错误消息:@“登录失败!”:0];
}
}否则{
//if(error)NSLog(@“error:%@”,error);
[自我提醒状态:@“连接失败”:@“登录失败!”:0];
}
}
}
@捕获(N例外*e){
NSLog(@“异常:%@”,e);
[自我提醒状态:@“登录失败”。:@“错误!”:0];
}
如果(成功){
[self-PerformsgueWithIdentifier:@“登录成功”发件人:self];
}
}
-(void)alertStatus:(NSString*)msg:(NSString*)title:(int)标记
{
UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:title
信息:msg
代表:赛尔夫
取消按钮:@“确定”
其他按钮类型:无,无];
alertView.tag=标签;
[警报视图显示];
}
-(iAction)背景点击:(id)发送者{
[自视图编辑:是];
}
-(BOOL)textField应返回:(UITextField*)textField{
[textField resignFirstResponder];
返回YES;
}
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后执行任何其他设置。
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
/*
#pragma标记-导航
//在基于故事板的应用程序中,您通常需要在导航之前做一些准备
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方
{
//使用[segue destinationViewController]获取新的视图控制器。
//将选定对象传递给新的视图控制器。
}
*/
@结束
LoginViewController.h

@interface LoginViewController : UIViewController <UITextFieldDelegate>


@property (strong, nonatomic) IBOutlet UIButton *signUpButton;
@property (strong, nonatomic) IBOutlet UIButton *forgotPasswordButton;
@property (strong, nonatomic) IBOutlet UITextField *passwordText;
@property (strong, nonatomic) IBOutlet UITextField *usernameText;

- (IBAction)loginClicked:(id)sender;
- (IBAction)backgroundTap:(id)sender;

@end
@interface LoginViewController:UIViewController
@属性(强,非原子)IBUIButton*signUpButton;
@属性(强,非原子)IBUIButton*forgotPasswordButton;
@属性(强,非原子)IBOutlet UITextField*passwordText;
@属性(强,非原子)IBOutlet UITextField*usernameText;
-(iAction)登录勾选:(id)发件人;
-(iAction)背景点击:(id)发送者;
@结束

这一行可能导致可能的问题。 success=[jsonData[@“success”]integerValue]; 它将success更新为大于零的值,即使在失败的情况下也是如此。 顺便说一下,你现在可以这样做来解决这个问题。希望它能起作用

  • (iAction)登录勾选:(id)发件人{ NSInteger成功率=0; @试一试
    if([[self.usernameText text] isEqualToString:@""] || [[self.passwordText text] isEqualToString:@""] ) {
    
        [self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0];
    
    } else {
        NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[self.usernameText text],[self.passwordText text]];
        NSLog(@"PostData: %@",post);
    
        NSURL *url=[NSURL URLWithString:@"http://www.techinworship.com/auth.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);
    
            NSError *error = nil;
            NSDictionary *jsonData = [NSJSONSerialization
                                      JSONObjectWithData:urlData
                                      options:NSJSONReadingMutableContainers
                                      error:&error];
    
            success = [jsonData[@"success"] integerValue];
            NSLog(@"Success: %ld",(long)success);
    
            if(success == 1)
            {
                NSLog(@"Login SUCCESS");
                [self performSegueWithIdentifier:@"login_success" sender:self];
    
            } else {
    
                NSString *error_msg = (NSString *) jsonData[@"error_message"];
                [self alertStatus:error_msg :@"Sign in Failed!" :0];
            }
    
        } else {
            //if (error) NSLog(@"Error: %@", error);
            [self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0];
        }
    }