在 AFHTTPRequestOperation 失败时显示 UIAlertView

Posted

技术标签:

【中文标题】在 AFHTTPRequestOperation 失败时显示 UIAlertView【英文标题】:Display UIAlertView on AFHTTPRequestOperation failure 【发布时间】:2015-03-22 03:36:20 【问题描述】:

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

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

错误的 JSON 响应是:

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

成功时 JSON 响应为:

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

登录视图控制器:

@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】:

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

【讨论】:

完美!有用。这是我需要知道的:NSDictionary *responseDictionary = responseObject;

以上是关于在 AFHTTPRequestOperation 失败时显示 UIAlertView的主要内容,如果未能解决你的问题,请参考以下文章

在 XCTests 中使用 AFHTTPRequestOperation

AFNetworking + AFHTTPRequestOperation 无法保存数组

如何在 AFNetworking 2 中执行 AFHTTPRequestOperation

为啥取消 AFHTTPRequestOperation 有时会遇到成功块?

即使操作队列被挂起,AFHTTPRequestOperation 仍在运行

AFHTTPRequestOperation 取消请求