Search code examples
objective-ciosuiapplicationdelegate

applicationWillResignActive dismiss keyboard iPhone


When an SMS is received during the use of my app, I would like for any open keyboards to be dismissed. How can I do that from applicationWillResignActive in my app delegate?


Solution

  • Implement code like the example in this answer. Have your view controllers register for UIApplicationWillResignActiveNotification. When the notification fires, call resignFirstResponder. That way you avoid tight coupling between your UIApplicationDelegate and your view controller. Assuming your view controller has a UITextField named textField:

    - (void) applicationWillResign {
        [self.textField resignFirstResponder];
    }
    
    - (void) viewDidLoad { 
        [[NSNotificationCenter defaultCenter]
            addObserver:self
            selector:@selector(applicationWillResign)
            name:UIApplicationWillResignActiveNotification 
            object:NULL];
    }