Q:

Keyboard and getting up state on iPhone

How do I find out if the keyboard is up?
I have a UISearchbar instance which becomes the first responder.
When the keyboard appears a notification is sent out as part of the API, however I don't want to respond to this right away. I could record this in a boolean state, but that seems clunky. I'd like to know if there is a "getter" some where I can call to find out.

A:

This is how I do it:
KeyboardStateListener.h
@interface KeyboardStateListener : NSObject {
    BOOL _isVisible;
}
+ (KeyboardStateListener *) sharedInstance;
@property (nonatomic, readonly, getter=isVisible) BOOL visible;
@end

KeyboardStateListener.m
#import "KeyboardStateListener.h"

static KeyboardStateListener *sharedObj;

@implementation KeyboardStateListener

+ (KeyboardStateListener *)sharedInstance
{
    return sharedObj;
}
+ (void)load
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    sharedObj = [[self alloc] init];
    [pool release];
}
- (BOOL)isVisible
{
    return _isVisible;
}
- (void)didShow
{
    _isVisible = YES;
}
- (void)didHide
{
    _isVisible = NO;
}
- (id)init
{
    if ((self = [super init])) {
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(didShow) name:UIKeyboardDidShowNotification object:nil];
        [center addObserver:self selector:@selector(didHide) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

-(void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

@end

Then use this to figure out the rest:
KeyboardStateListener *obj = [KeyboardStateListener sharedInstance];
if ([obj isVisible]) {
    //Keyboard is up
}

