Check iOS version at runtime?

前端 未结 11 1194
-上瘾入骨i
-上瘾入骨i 2020-12-01 03:03

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 notic

相关标签:
11条回答
  • 2020-12-01 03:12

    In MonoTouch:

    To get the Major version use:

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

    For minor version use:

    UIDevice.CurrentDevice.SystemVersion.Split('.')[1]
    
    0 讨论(0)
  • 2020-12-01 03:14

    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
    }
    
    0 讨论(0)
  • 2020-12-01 03:14

    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.

    0 讨论(0)
  • 2020-12-01 03:16

    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 讨论(0)
  • 2020-12-01 03:27

    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         
    
    }
    
    0 讨论(0)
  • 2020-12-01 03:30

    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).

    0 讨论(0)
提交回复
热议问题