【发布时间】: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];
}
}
【问题讨论】:
标签: ios json login uialertview afnetworking-2