iOS 9 - Keyboard pops up after UIAlertView dismissed
Asked Answered
R

2

5

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.

Rhymester answered 23/9, 2015 at 15:55 Comment(0)
V
16

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.

Volk answered 25/9, 2015 at 10:15 Comment(2)
Please note that UIAlertController only available after iOS 8. If your target includes iOS 7 users, handle it carefully.Gyneco
THANK YOU! saved my hours of banging my head on the wallCatamnesis
F
0

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)
Flatulent answered 28/8, 2016 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.