-1

I was wondering whether it is possible / how it is possible to code a class so it can be run on different iOS versions in Objective-C. For example the VisualEffectView is only available in iOS8 and after. Is it possible to declare a VisualEffectView if iOS >= 8 and UIView if not? If so can this be done within a header file?

I want to create an alert box to appear on top of a view controller when a save completes or error occurs. Depending on the iOS version it would be nice if a fancy blurry view is used or just a flat UIView.

matt
  • 515,959
  • 87
  • 875
  • 1,141
dgee4
  • 381
  • 3
  • 16

3 Answers3

1

In an if statement, use NSClassFromString. You'll discover immediately that UIVisualEffectView doesn't exist when it returns nil, and thus you can take one branch if it exists and another if it doesn't:

if (!NSClassFromString(@"UIVisualEffectView")) {
    // ... use UIView ...
} else {
    // ... use UIViewVisualEffectView ... {
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    And for a more extensive list of conditional coding techniques for backwards compatibility, see my book: http://www.apeth.com/iOSBook/ch09.html#_device_architecture_and_conditional_code – matt May 05 '15 at 16:03
1

As of iOS 5 you can the following syntax.

if ([UIVisualEffectView class]) {
    // Create and use a UIVisualEffectView
}

This will occasionally bite you, NSMapTable is available in iOS versions prior to iOS 6, but was only "officially" available in iOS 6. When attempting to use it in iOS 5 there was some sporadic undocumented behavior.

Tim Johnsen
  • 1,471
  • 14
  • 33
0

As many have suggested, you can use the NSClassFromString function to find out at run time if the OS version has the class. If it doesn't (that is iOS 7 devices) and you still want live blurring, I'd recommend LiveFrost.

rounak
  • 9,217
  • 3
  • 42
  • 59