2

According to the UITextInputTraits Protocol Reference, the UIKeyboardTypeDecimalPad is "Available in iOS 4.1 and later."

I am currently using the System Versioning Preprocessor Macros found in How to check iOS version? to check if my app is running in iOS 4.1 and later, but I wonder...

Is there a better way to test for this keypad's availability? I can't test for availability of the selector "setKeyboardType", since that will always return YES. I need to test if one of the UIKeyboardType enums is available for use.

Any ideas?

Community
  • 1
  • 1
RGoff
  • 117
  • 1
  • 9
  • Duplicate of [this](http://stackoverflow.com/questions/6468986/ios-objective-c-how-to-check-if-an-enum-is-available) question. – Hyperbole Dec 19 '11 at 17:06
  • @Hyperbole, your new comment beats checking the address of an enum. – RGoff Dec 19 '11 at 23:26

1 Answers1

4

Try this:

    yourTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    if (([[[UIDevice currentDevice] systemVersion] doubleValue] >= 4.1)) {
        yourTextField.keyboardType = UIKeyboardTypeDecimalPad;
    }

This is checking the iOS version running on the device to see if it is "new" enough. That is to say, the keypad with a decimal button was available in iOS4.1. So, any iOS version at or after that version has it.

The technique is (loosly) "capability testing based on version". There are other similar techniques of capability testing, such as checking if a class responds to a particular selector, that work well in other situations.

Generally, checking for the availability of a specific selector you want to use is better than checking version numbers. But in the case of something like a constant, checking the version of the underlying OS (in my opinion) is equally good.

Mark Granoff
  • 16,878
  • 2
  • 59
  • 61