开发者

How i can limit the number of tries to enter password, one can make to open an app

i want to limit the user from entering the password for n times and limit it to 3. How should i go 开发者_Python百科about this.

Thanks,


Just keep a variable counting the number of failed attempts, and when the user tries and fails too many times, do whatever you're going to do to block them from trying again.

That said, when the user makes too many failed attempts you probably just want to disable the input fields for a timeout period. You don't want to exit the application, and at some point you want them to be able to try again. Your goal is probably more like throttling the number of attempts they can make in a given period of time to make it harder for someone to break into an account they don't own.

So I would suggest that after the user fails too many times, disable the UITextFields you're using for inputs, then set a timer for some period of time, and when the timer goes off, re-enable the text fields:

 - (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;
        });
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜