Luke
Luke

Reputation: 9700

iOS 9 - Keyboard pops up after UIAlertView dismissed

I have a strange visual bug that only affects iOS 9 devices:

My app’s login UIViewController runs off and gets an OAuth token when you hit the button, much like you’d expect. If the response from my API returns a specific status code, I pop up a UIAlertView saying they need to reset their password (this is if they’ve been flagged as such on the server end). The email and password fields for login resignFirstResponder once you hit the button, standard stuff.

On iOS 9 only, if you hit the reset path, the second you tap OK on that alert view, the keyboard pops back up, for maybe 800ms, then dismisses again. It’s almost as if something was queued to present it, but the presence of the alert blocked it until you hit OK – it’s absolutely instantaneous after hitting okay on the alert.

It seems a really tricky one to debug. I’ve added symbolic breakpoints to becomeFirstResponder and it isn’t called anywhere near this process occurring.

Any other ideas for how I can look at debugging / fixing this? It doesn’t affect iOS 7 and iOS 8, only iOS 9.

Upvotes: 5

Views: 3780

Answers (2)

Cesar Varela
Cesar Varela

Reputation: 5114

Here is an extension to handle this in swift 3

extension UIViewController {

    func presentOk(with title: String, and message: String, handler: ((UIAlertAction) -> Void)?) {

        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: handler))

        OperationQueue.main.addOperation {
            self.view.endEditing(true)
            self.present(alert, animated: true, completion: nil)
        }
    }
}

The key is to hide the keyboard and present the controller in the main queue.

Usage

presentOk(with: "My app title", and: "this is the alert message", handler: nil)

Upvotes: 0

cmart
cmart

Reputation: 211

I faced this problem about 30 minutes ago.

The UIAlertView has been deprecated since iOS9 was released.

We solved this issue by using the UIAlertController, like this:

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert Title!" message:@"This is an alert message." preferredStyle:UIAlertControllerStyleAlert];

     UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
     [alertController addAction:ok];

     [self presentViewController:alertController animated:NO completion:nil];

This should fix your problem.

If animated = YES, you may get the same issue as before. This is a bug with iOS9.

Let me know how it goes, and if this fixes your problem.

Upvotes: 16

Related Questions