Neigaard
Neigaard

Reputation: 4050

Only run code if iOS < 6.0 with preprocessing?

I can find many examples on how to only compile/run code then iOS version is > something, but how do I do it the other way around? I tried the following by running iOS 5.0 in the simulator:

#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_6_0
// Code for iOS < 6.0 here
#endif

But the code inside the #if - #endif is not run on iOS 5.0 in the simulator. How can I do this?

[EDIT] Ok so I wasn't sure what I wanted it seems, sorry :) The thing is that I want this code in my UITableViewDelegate to be run only if the device is running iOS < 6.0:

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
}

This is because I want to do some styling if I am running iOS < 6, but on iOS 6 I can do this styling much much easier. But a version check at runtime inside this method is not really what I want because then it is to late.

Thank you
Søren

Upvotes: 0

Views: 1448

Answers (2)

nielsbot
nielsbot

Reputation: 16022

There's a difference between

  1. a compile-time check if you're compiling against a specific SDK and
  2. checking which OS your code is running on

Checking for the __IPHONE_6_0 macro will just check which target you're compiling for... is that what you want? If so, you could use #ifndef __IPHONE_6_0 to check if you are not compiling for iOS 6.

If you want to know which OS your code is running on, you can check MSK's answer.

Upvotes: 7

msk
msk

Reputation: 8905

Here is a run time not compile time check.

#define SYSTEM_VERSION_LESS_THAN(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

use it like

if(SYSTEM_VERSION_LESS_THAN(@"6.0")) {

}

Upvotes: 3

Related Questions