views:

128

answers:

2

I building an app targeting iOS 3.1.3 and later and I've run into a problem with UIKeyboardBoundsUserInfoKey. Turns out it is deprecated in iOS 3.2 and later. What I did was use the following code to use the right key depending on the iOS version:

if ([[[UIDevice currentDevice] systemVersion] compare:@"3.2" options:NSNumericSearch] != NSOrderedAscending)
    [[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
else [[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];

And this actually works fine, but Xcode warns me that UIKeyboardBoundsUserInfoKey is deprecated. How can I get rid of this warning without having to suppressing any other warnings?

Also, is there a way to simply check if UIKeyboardBoundsUserInfoKey is defined to avoid having to check the iOS version? I tried checking if it was NULL or nil and even weaklinking UIKit but nothing seemd to work.

Thanks in advance

A: 

http://stackoverflow.com/questions/2807339/uikeyboardboundsuserinfokey-is-deprecated-what-to-use-instead/2807367#2807367

Jared P
I know what to use instead of UIKeyboardBoundsUserInfoKey, what I'm asking is how to maintain compatibility with 3.1.3 without getting a warning from Xcode.
Pablo
+1  A: 

Since the presence of a deprecated constant anywhere on your code would raise a warning (and break the build for us -Werror users), you can use the actual constant value to lookup the dictionary. Thank Apple for usually (always?) using the constant name as it's value.

As for the runtime check, I think you're better testing for the new constant:

&UIKeyboardFrameEndUserInfoKey!=nil

So, this is what I'm actually doing to get the keyboard frame (based on this other answer):

-(void)didShowKeyboard:(NSNotification *)notification {
    CGRect keyboardFrame = CGRectZero;

    if (&UIKeyboardFrameEndUserInfoKey!=nil) {
        // Constant exists, we're >=3.2
        [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
        if (UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
            _keyboardHeight = keyboardFrame.size.height;
        }
        else {
            _keyboardHeight = keyboardFrame.size.width;
        }   
    } else {
        // Constant has no value. We're <3.2
        [[notification.userInfo valueForKey:@"UIKeyboardBoundsUserInfoKey"] getValue: &keyboardFrame];
        _keyboardHeight = keyboardFrame.size.height;
    }
}

I actually tested this on a 3.0 Device and 4.0 Simulator.

leolobato
Thanks! This is exactly what I was looking for.
Pablo