程序崩溃的API调用iphone

时间:2013-02-25 18:19:17

标签: iphone json api

我有两页:

登录页面和注册页面:

对于登录页面,我使用以下内容获取信息并将失败或成功响应传递给用户:

- (void)login:(UIButton *)sender
{
    [self.appDel.api loginWithEmail:self.email.text
                           password:self.password.text
                  completionHandler:^(id response, NSError *error) {
                      if (error) {
                          NSLog(@"ERROR: %@", error);
                      } else {
                          if ([[response objectForKey:Status] isEqualToString:Success]) {
                              self.currentUser = [response objectForKey:kResponseUser];
                              self.currentUserProfile = [response objectForKey:Profile];
                              [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
                          } else {
                              NSLog(@"FAIL: %@", response);
                              [[[UIAlertView alloc] initWithTitle:@"Login Failed"
                                                          message:[response description]
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil] show];
                          }
                      }
                  }];
}

将此用于登录页面,如果用户没有完成两个必需的texfields或者其中一个填充另一个空白并按下Login,他将收到失败消息。

在注册案例中,当文本字段为空时程序崩溃,我们点击注册。我该怎么改变这个:

-(void)signUp:(UIButton *)sender
{
    [self.api signup:self.Email.text
                                   password:self.Password.text
                       confirmationPassword:self.ConfirmPassword.text
                                  firstName:self.FirstName.text
                          completionHandler:^(id response, NSError *error) {
    if (error) {
        NSLog(@"ERROR: %@", error);
    } else {
        if ([[response objectForKey:Status] isEqualToString:Success]) {
        self.appDel.theNewUser = [response objectForKey:User];
        self.appDel.theNewUserProfile = [response objectForKey:Profile];
            [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
        } else {
            NSLog(@"FAIL: %@", response);
            [[[UIAlertView alloc] initWithTitle:@"SignUp Failed"
                                        message:[response description]
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
        }
    }
}];

}

1 个答案:

答案 0 :(得分:0)

在您的注册方法中(或者更好的是,在您的API类中),检测nil和空值。现在的问题是你要在字典中插入一个零值。

以下是使用您的代码的示例。我检查是否有任何值长度等于零,如果是,则记录一点错误。

-(void)signUp:(UIButton *)sender
{

    if ([[[self Email] text] length] == 0 ||
        [[[self Password] text] length] == 0 ||
        [[[self ConfirmPassword] text] length] == 0 ||
        [[[self FirstName] text] length] == 0) {

        // add an error alert here?

        NSLog(@"Hold on there!  You haven't filled everything in.");

        return;

    }

    [self.api signup:self.Email.text
                                   password:self.Password.text
                       confirmationPassword:self.ConfirmPassword.text
                                  firstName:self.FirstName.text
                          completionHandler:^(id response, NSError *error) {
    if (error) {
        NSLog(@"ERROR: %@", error);
    } else {
        if ([[response objectForKey:Status] isEqualToString:Success]) {
        self.appDel.theNewUser = [response objectForKey:User];
        self.appDel.theNewUserProfile = [response objectForKey:Profile];
            [self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
        } else {
            NSLog(@"FAIL: %@", response);
            [[[UIAlertView alloc] initWithTitle:@"SignUp Failed"
                                        message:[response description]
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
        }
    }
}];

}