1

Sometimes there are features added in new versions of iOS that are tempting to use, but you don't want to prevent users on older devices from using the app just because of one minor feature.

I've just added UIRefreshControl to an app, which was added in iOS 6. I don't really care enough to use a 3rd party solution (of which there are a number), I'd just like to not invoke the code that sets it up if I'm running on an older device. I suspect, being a static language, this isn't possible, but I thought I'd ask and see if there are ways of achieving this.

Obviously if I was just sharing the code I could do this with preprocessor macros, but I need to build the app to execute on iOS 5.1 + iOS 6.0.

d11wtq
  • 34,788
  • 19
  • 120
  • 195

1 Answers1

5

This is perfectly possible, just check if a class is available:

if ([UIRefreshControl class]) {
   // the UIRefreshControl is available.
}

It the same with new method on existing classes:

if ([self.someLabel  respondsToSelector:@selector(setAttributedText:)]) {
    // The label support attributed strings
}

Objective-c is not a static as you might think, just be sure that you weak link any frameworks that are not available in earlier versions of iOS.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Wow, just wow. I just had one of those "oh, how cool" moments, as I thought about how the ObjC code gets translated into C function calls into the ObjC runtime. That is sweet. – d11wtq Jan 29 '13 at 12:28