也就是说,当用户尝试太多失败时,您可能只想在一个超时时间内禁用输入字段。你不想退出应用程序,在某个时候你希望他们能够再试一次。你的目标可能更像是限制他们在一段时间内可以尝试的次数,以使某人更难打入自己并不拥有的账户。
因此,我建议在用户失败太多次后,禁用用于输入的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;
});
}
}