0

Hi I would like to hide a block of code in one of my project. Hidding means I don't want to execute a block of code in one IOS version like IOS6 and want to execute in another version like in IOS7.Why I would like to do this is because any IOS developer know one universal truth that is most of the times Apple introduce new features in new versions due to that there may be changes in SDk.And if we want to run newer version app in older version compiler will give errors because new version related methods won't be available in older version. So in order to avoid these errors I need one approach for hiding a block of code which is related to new version while running app in older version.

So If anyone know please let me know.Thanks in advance.

Naresh
  • 363
  • 1
  • 18

4 Answers4

0

Either use respondsToSelector method or check for system version using [[UIDevice currentDevice] systemVersion].

respondsToSelector is a method provided by NSObject protocol.
1.

if ([str respondsToSelector:@selector(sizeWithFont:)]) {
            //do something
        }
    else{
    //do other thing
    }

2.

 NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"6.0" options: NSNumericSearch];
    if (order == NSOrderedSame || order == NSOrderedDescending) {
        // OS version >= 6.0
    } else {
        // OS version < 6.0
    }
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
  • Hi puneet Thanks for your quick response. Actually I used the method which you recommanded but I am getting errors in IOS6.I mean for example I wrote a condition to execute [self setNeedsStatusBarAppearanceUpdate] in IOS 7.0.It's working fine in Xcode 4.5 with IOS SDk7.0.But If I run the same code in xcode 4.6 using IOS SDK 6.0 It's giving error as such @"No Visible @interface for "Class name here" declares the selector 'setNeedsStatusBarAppearanceUpdate'" Like this so please help to resolve this issue. – Naresh Sep 27 '13 at 06:13
  • 1
    @Gadamsetty, I think you're conflating the SDK you build against with the OS you're targeting. If you need your code to build against the older SDK, then you'll need to use a compile-time check with `#ifdef` instead of (or in addition to) a run-time check like Puneet suggests. – dpassage Sep 27 '13 at 06:32
0

You can do something like:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
    // your code
}
Chris
  • 489
  • 5
  • 15
0

You can always check for the version of OS. Take a look at this question. It will solve your problem.

Community
  • 1
  • 1
Ratikanta Patra
  • 1,177
  • 7
  • 18
0

You can use systemVersion property of UIDevice Class.

Like:

if ([[[UIDevice currentDevice] systemVersion] floatValue] <= 6 )
{
    if ([yourObject respondsToSelector:@selector(yourMethod)])
    {
       [yourObject performSelector:@selector(yourMethod)];
    }
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200