Search code examples
iphoneversion-controlversionconditional-compilation

#ifdef macros for versions controlling


I use macros to differ the versions but I can't force it to work properly. I used:

#ifdef _IPHONE_4_0
  [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
 #else
  [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
 #endif

and

#if __IPHONE_OS_VERSION_MAX_ALLOWED < _IPHONE_4_0
  [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
 #else
  [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
 #endif

and

#if defined(__IPHONE_4_0)
  [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
 #else
  [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
 #endif

No matter what version I use - always called only one of the lines. And the __IPHONE_4_0 is always defined. Any ideas?


Solution

  • The #if… processor directives are resolved at compile time. As long as you compile for the 4.0 SDK, the 4.0 variant will always be chosen.

    If you intend to make the app works for < 4.0, you should use a runtime check:

    UIApplication* app = [UIApplication sharedApplication];
    if ([app respondsToSelector:@selector(setStatusBarHidden:withAnimation:)])
      [app setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    else
      [app setStatusBarHidden:YES animated:YES];