Deprecated constant in iOS

I am building an application targeting iOS 3.1.3 and later, and I am having a problem with UIKeyboardBoundsUserInfoKey . It turns out that it is deprecated in iOS 3.2 and later. I used the following code to use the right key depending on the version of iOS:

 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 great, but Xcode warns me that UIKeyboardBoundsUserInfoKey deprecated. How can I get rid of this warning without having to suppress any other warnings?

Also, is there a way to simply check if a UIKeyboardBoundsUserInfoKey exists to avoid having to check the iOS version? I tried to check if it was NULL or nil and even a weak UIKit link, but nothing works.

Thanks in advance

+4
source share
2 answers

Since the presence of an outdated constant anywhere in your code will cause a warning (and break the assembly for us - Werror users), you can use the actual value of the constant to search for the dictionary. Thanks to Apple for the usual (always?) Using constant name as value.

As for runtime checking, I think you're better off testing a 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 really tested this on a 3.0 device and 4.0 simulator.

+4
source

Source: https://habr.com/ru/post/1314505/


All Articles