문제

My app was previously coded in XCode 4.5, now for iOS7 compatibility, I'm coding it into XCode 5.0, have added some code, that start support from iOS7 only. At the same time I need to run it back on XCode 4.5, now here it'll show error because that particular code not available in iOS < 7.0.

E.g.

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

There's no issue when I compile it with XCode 5.0 but showing error when compile in XCode 4.5, I know the reason -- that is,UIStatusBarStyleLightContent is available from iOS7 only, and iOS7 is starting support from XCode 5.0 onwards, but I want a solution that this code won't affect if its not support for particular iOS target?

Something like,

if(NOT_SUPPORTED_IOS) {
   //NO NEED TO COMPILE THIS LINE or THIS LINE WONT EXECUTE
}
도움이 되었습니까?

해결책

You can use conditional compiling to handle such cases:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 // iOS 7.0 supported
  // iOS 7 code here
#else
  // Pre-iOS 7 code here
#endif

When you compile under Xcode 5, you will be compiling against the iOS 7 SDK: in such case, the first branch is used. When you compile under Xcode 4.5, you will be using an older SDK version and the second branch will be used.

Notice the use of precompiler #if/#endif: this will effectively make code visible or not visible to the compiler.

This will only fix the issue at compile time. But you still have another issue to consider: when your Xcode 5 built app will be run on an, e.g., iOS 6 device. In this case you app will crash, because of the use of the iOS 7-only feature. So you also need a runtime guard like:

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

In your case, this would give:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 // iOS 7.0 supported
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
      [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    else
#endif
      [UIApplication sharedApplication].statusBarStyle = ...;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top