4

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

Kampai
  • 22,848
  • 21
  • 95
  • 95
Pablo
  • 317
  • 5
  • 8

2 Answers2

4

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.

Community
  • 1
  • 1
leolobato
  • 2,359
  • 3
  • 32
  • 51
  • 1
    In case anyone else comes across this code, be aware that it is not quite what you're looking for. The keyboardFrame returned doesn't take account of interface orientation, and the if (UIInterfaceOrientationIsPortrait) line will give you the wrong answer in various circumstances (notably when the device is flat on the table instead of being held upright). Instead what you want to do is use [self.view convertRect:keyboardFrame fromView:nil] to convert from untransformed window coordinates to view coordinates. This will rotate the frame such that the height is always the right one to use. – MrCranky Jan 31 '11 at 13:45
0

UIKeyboardBoundsUserInfoKey is deprecated, what to use instead?

Community
  • 1
  • 1
Jared Pochtar
  • 4,925
  • 2
  • 29
  • 39
  • 1
    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 Jul 02 '10 at 02:09