在AFHTTPRequestOperation失败时显示UIAlertView

时间:2015-03-22 03:36:20

标签: ios json login uialertview afnetworking-2

我正在尝试在AFHTTPRequestOperation的失败块中显示UIAlertView,当用户登录时JSON响应是错误。我不确定为什么我的代码不起作用,但我是新的到AFNetworking。

出于某种原因,UIAlertView成功即使出现错误也是如此。即使我没有在UITextFields中输入任何内容进行登录。如何在故障或错误块中显示UIAlertView?

错误时的JSON响应是:

JSON: {
   code = Error;
   message = "Incorrect Username or Password!";
}

成功时,JSON响应为:

JSON: {
   code = Success;
   message = "Login Successful!";
}

LoginViewController:

@interface LoginSectionViewController ()

@property (strong, nonatomic) IBOutlet UIButton *loginButton;
@property (strong, nonatomic) IBOutlet UITextField *usernameTextField;
@property (strong, nonatomic) IBOutlet UITextField *passwordTextField;

@end

- (IBAction)loginAction:(id)sender {

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"username": self.usernameTextField.text, @"password": self.passwordTextField.text};

  [manager POST:@"http://api.mysite.com/login.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                                    message:@"Login Successful!"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    UIAlertView *alertError = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"Incorrect Username or Password!"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
     [alertError show];
  }];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
       [self.navigationController popToRootViewControllerAnimated:YES];
   }
}

1 个答案:

答案 0 :(得分:1)

successfailure块用于指示请求中的基本错误(例如,请求失败,因为没有Internet连接,响应不是JSON并且解析失败等)。但它并没有关注JSON内容本身。

这取决于你。在success块中(即“成功收到JSON响应”),您可以检查JSON的内容,以向用户指示登录是否成功。


我没有测试过这个,但希望这说明了这个想法:

[manager POST:@"http://api.mysite.com/login.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);

    NSDictionary *responseDictionary = responseObject;

    if ([responseDictionary[@"code"] isEqualToString:@"Success"]) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                                        message:@"Login Successful!"
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    } else {
        UIAlertView *alertError = [[UIAlertView alloc] initWithTitle:@"Login failed"
                                                             message:responseDictionary[@"message"]
                                                            delegate:self
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil];
        [alertError show];
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    UIAlertView *alertError = [[UIAlertView alloc] initWithTitle:@"Network problem"
                                                        message:[error localizedDescription]
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    [alertError show];
}];