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

时间:2010-11-08 13:51:03

标签: xcode

我想限制用户输入密码n次并将其限制为3。 我应该怎么做呢。

谢谢,

1 个答案:

答案 0 :(得分:0)

只需保留一个变量来计算失败的尝试次数,当用户尝试失败次数太多时,做你要做的任何事情来阻止他们再次尝试。

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

所以我建议在用户失败太多次后,禁用你用于输入的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;
        });
    }
}