gpt4 book ai didi

ios - 在 AFHTTPRequestOperation 失败时显示 UIAlertView

转载 作者:行者123 更新时间:2023-11-29 12:25:45 24 4
gpt4 key购买 nike

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

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

错误的 JSON 响应是:

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

成功时 JSON 响应为:

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

登录 View Controller :

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

最佳答案

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

这取决于你。在您的 success block (即“成功收到 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];
}];

关于ios - 在 AFHTTPRequestOperation 失败时显示 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29190682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com