Search code examples
iosjsonauthenticationuialertviewafnetworking-2

Display UIAlertView on AFHTTPRequestOperation failure


I'm trying to show a UIAlertView in the failure block of the AFHTTPRequestOperation when the JSON response is an error when a user is logging in. I'm not exactly sure why my code isn't working, but I am new to AFNetworking.

For some reason the UIAlertView for success shows even on error. Even if I don't input anything into the UITextFields for the login. How do I get the UIAlertView in the failure or error block to show?

The JSON response on error is:

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

On success the JSON response is:

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];
   }
}

Solution

  • The success and failure blocks are to indicate fundamental errors in the request (e.g. the request failed because there was no Internet connection, the response wasn't JSON and failed parsing, etc.). But it's not looking at the JSON contents itself.

    That's up to you. In your success block (i.e. "successfully received JSON response") you can then examine the contents of the JSON to indicate to the user whether the login succeeded or not.


    I haven't tested this, but hopefully this illustrates the idea:

    [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];
    }];