ansonl
ansonl

Reputation: 786

Check if UIBarButtonSystemItemPageCurl exists in iOS version

I am trying to check if UIBarButtonSystemItemPageCurl exists on a given device.

Note: UIBarButtonSystemItemPageCurl exists on iOS 4.0 and above. I am also targeting iOS 3.1.3. Don't worry about the targeting iOS 3 part.

Currently tried: if (UIBarButtonSystemItemPageCurl != NULL)

and

if ((UIBarButtonSystemItemPageCurl) == TRUE)

to check existence of the constant (Is UIBarButtonSystemItemPageCurl considered a constant? It's a value of typedef enum UIBarButtonSystemIcon). These two methods are currently not working. Can someone provide guidance on checking the existence a value of in a struct (not the containing struct)? Thanks.

Upvotes: 0

Views: 454

Answers (1)

Peter Hosey
Peter Hosey

Reputation: 96373

If you look in UIBarButtonItem.h, you'll find that UIBarButtonSystemItemPageCurl is defined conditionally using the preprocessor:

typedef NS_ENUM(NSInteger, UIBarButtonSystemItem) {
    ⋮
#if __IPHONE_4_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
    UIBarButtonSystemItemPageCurl,
#endif
};

…_MAX_ALLOWED is defined to the SDK version. Once the constant is defined, the constant always exists.

Comparing it against NULL is meaningless because this isn't a pointer. You are effectively comparing it against zero, and as it isn't the first thing in the enumeration, it isn't zero, so it is never NULL.

What it is is an integer. UIBarButtonSystemItemPageCurl is synonymous with 23, and the number 23 always exists, regardless of OS version.

So the question becomes “is UIBarButtonSystemItemPageCurl (a.k.a. 23) something that UIKit will recognize?”

One way to find that out would be to pass it to initWithBarButtonSystemItem:target:action: and see what happens. Hopefully, it will either return nil or throw an exception; either way, you can detect that and recover by doing whatever you need to do on iOS 3 devices.

Another way would be to ask the device for its systemVersion and compare it to @"4.0" in a way that understands version numbers. The Growl project has code for parsing and comparing version number strings. It's written for OS X, but should work with little to no modification on iOS.

I'd do the try-it-and-see approach first. Only if it silently fails (i.e., always returns a UIBarButtonItem, even on iOS 3, and the item you get on 3 just doesn't work) should you resort to comparing OS versions.

Upvotes: 2

Related Questions