代码之家  ›  专栏  ›  技术社区  ›  Ashutosh

我如何能限制尝试输入密码的次数,一个人可以做打开一个应用程序

  •  0
  • Ashutosh  · 技术社区  · 16 年前

    我想限制用户输入密码n次,并限制为3次。 我该怎么办。

    1 回复  |  直到 16 年前
        1
  •  0
  •   Ryan    16 年前

    也就是说,当用户尝试太多失败时,您可能只想在一个超时时间内禁用输入字段。你不想退出应用程序,在某个时候你希望他们能够再试一次。你的目标可能更像是限制他们在一段时间内可以尝试的次数,以使某人更难打入自己并不拥有的账户。

    因此,我建议在用户失败太多次后,禁用用于输入的UITextFields,然后设置一段时间的计时器,当计时器关闭时,重新启用文本字段:

     - (void)_loginFailed
    {
        _failedLoginAttemptCount++;
        if (failedLoginAttemptCount >= 3) {
            _usernameTextField.enabled = NO;
            _passwordTextField.enabled = NO;
    
            // Use dispatch_after() to execute a block of code after a timeout.  
            // By using dispatch_after() with dispatch_walltime(), we can be sure that our timeout will count time spent with the app suspended in case the user presses the Home button on the device to go to another app while they wait.
            dispatch_after(dispatch_walltime(NULL, 60 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                // The timeout has passed, so re-enable the text fields and reset the failed logging attempt count.
                _failedLoginAttemptCount = 0;
                _usernameTextField.enabled = YES;
                _passwordTextField.enabled = YES;
            });
        }
    }