1

I am using conditional code as below,

I want to run certain code only in ios5.0 and > ios5.0( i mean i want to support ios5.0 and 5.1 version too)

But the below condition dos not seem to work. ( Currently my development version is 5.1 but the below snippet is not getting identified.the control is not going into it.)

Please let me know your thoughts

#ifdef __IPHONE_5_0_OR_LATER
user198725878
  • 6,266
  • 18
  • 77
  • 135
  • 1
    maybe this could help: http://stackoverflow.com/questions/3339722/check-iphone-ios-version – CarlJ May 25 '12 at 08:34
  • both answers below ar correct - but it seems that you want to check version during runtime (possibly at application startup?) - in that case take a look at the link that meccan provided – Rok Jarc May 25 '12 at 08:38
  • possible duplicate of [How to target a specific iPhone version?](http://stackoverflow.com/questions/820142/how-to-target-a-specific-iphone-version) – Paul R May 25 '12 at 08:39

3 Answers3

4
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
// iPhone 5.0 code here
#endif


#define __IPHONE_2_0     20000
#define __IPHONE_2_1     20100
#define __IPHONE_2_2     20200
#define __IPHONE_3_0     30000
#define __IPHONE_3_1     30100
#define __IPHONE_3_2     30200
#define __IPHONE_4_0     40000
#define __IPHONE_4_1     40100
#define __IPHONE_4_2     40200
#define __IPHONE_4_3     40300
#define __IPHONE_5_0     50000
#define __IPHONE_5_1     50100
#define __IPHONE_NA      99999  /* not available */

How to target a specific iPhone version?

Community
  • 1
  • 1
adali
  • 5,977
  • 2
  • 33
  • 40
  • Be aware that is only compile time directive, thus if you set you target to a lower versie you can not use this to make you code switch between two methods depending on the os version. It will take the base SDK in the `#ifdef` and will crash if you call no existing methods on older iOS versions. – rckoenes May 25 '12 at 08:41
2

#ifdef is a compile directive, thus it will be evaluated at compile time not run time.

Thus if you add this to you code, the methods call in the if will all ways be called if your target SDK matches your #ifdef. So if you compile an app for both iOS 4 and 5 and place all the 5 only methods in #ifdef io5 the app will crash on iOS 4 since the methods will be called.

If you want to check if some method is available then you should do like :

Here is an example for dismissing an modal view controller from it's parent. Since parentViewController is changed to presentingViewController in iOS 5, we check if presentingViewController is available and use it.

if ([self respondsToSelector:@selector(presentingViewController)]) {
    [self.presentingViewController dismissModalViewControllerAnimated:YES];
} else {
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}

The same with goes for checking if a class is available :

if ([MPNowPlayingInfoCenter class]) {
     MPNowPlayingInfoCenter *center = [MPNowPlayingInfoCenter defaultCenter];
     NSDictionary *songInfo = /* ... snip ... */;
    center.nowPlayingInfo = songInfo;
}
rckoenes
  • 69,092
  • 8
  • 134
  • 166
0
NSArray *versionCompatibility = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];

if ( 5 == [[versionCompatibility objectAtIndex:0] intValue] ) { /// iOS5 is installed

    // Put iOS-5 code here

} else { /// iOS4 is installed

    // Put iOS-4 code here         

}
Mani
  • 1,841
  • 15
  • 29
  • You should not do it this way. because your code is not future prove. If Apple decides to remove a class or change a method your app will crash. – rckoenes May 25 '12 at 11:22