开发者

Check iOS version at runtime?

This is sort of a follow on from my last question. I am using beginAnimations:context: to setup an animation block to animate some UITextLabels. However I noticed in the docs that is says: "Use of this method is discouraged in iOS 4.0 and later. Yo开发者_Go百科u should use the block-based animation methods instead."

My question is I would love to use animateWithDuration:animations: (available in iOS 4.0 and later) but do not want to exclude folks using iOS 3.0. Is there a way to check to iOS version of a device at runtime so that I can make a decision as to which statement to use?


Simpler solution for anyone who'll need help in the future:

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         

}


In many cases you do not need to check iOS version directly, instead of that you can check whether particular method is present in runtime or not.

In your case you can do the following:

if ([[UIView class] respondsToSelector:@selector(animateWithDuration:animations:)]){
// animate using blocks
}
else {
// animate the "old way"
}


to conform to version specified in system defines

//#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
You can write function like this ( you should probably store this version somewhere rather than calculate it each time ):

+ (NSInteger) getSystemVersionAsAnInteger{
    int index = 0;
    NSInteger version = 0;

    NSArray* digits = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
    NSEnumerator* enumer = [digits objectEnumerator];
    NSString* number;
    while (number = [enumer nextObject]) {
        if (index>2) {
            break;
        }
        NSInteger multipler = powf(100, 2-index);
        version += [number intValue]*multipler;
        index++;
    }
return version;
}

Then you can use this as follows:

if([Toolbox getSystemVersionAsAnInteger] >= __IPHONE_4_0)
{
  //blocks
} else 
{
  //oldstyle
}


Xcode 7 added the available syntax making this relatively more simple:

Swift:

if #available(iOS 9, *) {
    // iOS 9 only code
} 
else {
   // Fallback on earlier versions
}

Xcode 9 also added this syntax to Objective-C

Objective-C:

if (@available(iOS 9.0, *)) {
   // iOS 9 only code
} else {
   // Fallback on earlier versions
}


Most of these solutions on here are so overkill. All you need to do is [[UIDevice currentDevice].systemVersion intValue]. This automatically removes the decimal, so there is no need to split the string.

So you can just check it like:

if ([[UIDevice currentDevice].systemVersion intValue] >= 8) {
    // iOS 8.0 and above
} else {
    // Anything less than iOS 8.0
}

You can also define a macro with this code:

#define IOS_VERSION [[UIDevice currentDevice].systemVersion intValue];

or even include your check:

#define IOS_8PLUS ([[UIDevice currentDevice].systemVersion intValue] >= 8)

Then you just need to do:

if (IOS_8PLUS) {
    // iOS 8.0 and above
} else {
    // Anything less than iOS 8.0
}


Discouraged is not the same as deprecated.

If you need to support earlier versions of iOS that do not have the block based methods, there is nothing wrong with using the older methods (as long as they haven't been removed, of course).


You can use the version of the Foundation framework to determine the current system version.

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1){

//for earlier versions

} else {

//for iOS 7

}


For my purposes I've written a tiny library that abstracts away the underlying C calls and presents an Objective-C interface.

GBDeviceDetails deviceDetails = [GBDeviceInfo deviceDetails];
if (deviceDetails.iOSVersion >= 6) {
    NSLog(@"It's running at least iOS 6");      //It's running at least iOS 6
}

Apart from getting the current iOS version, it also detects the hardware of the underlying device, and gets info about the screen size; all at runtime.

It's on github: GBDeviceInfo. Licensed under Apache 2.


Put this in your Prefix.pch file

#define IOS_VERSION [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] firstObject] intValue]

And then you can check iOS versions like:

if(IOS_VERSION == 8)
{
     // Hello 8!
}
else
{
     // Hello some other version!
}

Off course if you can use feature detection (and it makes sense for your use case) you should do that.


In MonoTouch:

To get the Major version use:

UIDevice.CurrentDevice.SystemVersion.Split('.')[0]

For minor version use:

UIDevice.CurrentDevice.SystemVersion.Split('.')[1]


A bit nicer and more efficient adaptation to the above solutions:

-(CGPoint)getOsVersion
{
    static CGPoint rc = {-1,-1};
    if (rc.x == -1) {
        NSArray *versionCompatibility = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
        rc.x = [versionCompatibility[0] intValue];
        rc.y = [versionCompatibility[1] intValue];
    }
    return rc;
}

now you can

if ([self getOsVersion].x < 7) {
}

HTH

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜