Wednesday, May 30, 2012

Check iPhone iOS Version


I want to check if the iOS version of the device is greater then the 3.1.3 I tried things like:




[[UIDevice currentDevice].systemVersion floatValue]



but does not work, I just want a:




if (version > 3.1.3) { }



How can I do this?


Source: Tips4all

6 comments:

  1. You can get the OS version using:

    [[UIDevice currentDevice] systemVersion]

    However, you should avoid relying on the version string as an indication of device or OS capabilities. There is usually a more reliable method of checking whether a particular feature or class is available. For example, you can check if UIPopoverController is available on the current device using NSClassFromString:

    if(NSClassFromString(@"UIPopoverController")) {
    // Do something
    }


    Some classes, like CLLocationManager and UIDevice, provide methods to check device capabilities:

    if([CLLocationManager headingAvailable]) {
    // Do something
    }


    Apple uses systemVersion in their GLSprite sample code, so my recommendation can't be absolute:

    // A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
    // class is used as fallback when it isn't available.
    NSString *reqSysVer = @"3.1";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
    displayLinkSupported = TRUE;


    Important Note:
    If for whatever reason you decide that systemVersion is what you want, make sure to treat it as an string or you risk truncating the minor revision number (eg. 3.1.2 -> 3.1).

    ReplyDelete
  2. /*
    * System Versioning Preprocessor Macros
    */

    #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
    #define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
    #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

    /*
    * Usage
    */

    if (SYSTEM_VERSION_LESS_THAN(@"4.0")) {
    ...
    }

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"3.1.1")) {
    ...
    }

    ReplyDelete
  3. I recommend:

    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 3.13) {
    ; // ...
    }


    credit: http://stackoverflow.com/questions/820142/how-to-target-a-specific-iphone-version

    ReplyDelete
  4. Try this blog:
    http://cocoawithlove.com/2010/07/tips-tricks-for-conditional-ios3-ios32.html

    ReplyDelete
  5. Try:

    NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"3.1.3" options: NSNumericSearch];
    if (order == NSOrderedSame || order == NSOrderedDescending) {
    // OS version >= 3.1.3
    } else {
    // OS version < 3.1.3
    }

    ReplyDelete
  6. In general it's better to ask if an object can perform a given selector, rather than checking a version number to decide if it must be present.

    When this is not an option, you do need to be a bit careful here because [@"5.0" compare:@"5" options:NSNumericSearch] returns NSOrderedDescending which might well not be intended at all; I might expect NSOrderedSame here. This is at least a theoretical concern, one that is worth defending against in my opinion.

    Also worth considering is the possibility of a bad version input which can not reasonably be compared to. Apple supplies the three predefined constants NSOrderedAscending, NSOrderedSame and NSOrderedDescending but I can think of a use for some thing called NSOrderedUnordered in the event I can't compare two things and I want to return a value indicating this.

    What's more, it's not impossible that Apple will some day expand their three predefined constants to allow a variety of return values, making a comparison != NSOrderedAscending unwise.

    With this said, consider the following code.

    typedef enum {kSKOrderedNotOrdered = -2, kSKOrderedAscending = -1, kSKOrderedSame = 0, kSKOrderedDescending = 1} SKComparisonResult;

    @interface SKComparator : NSObject
    + (SKComparisonResult)comparePointSeparatedVersionNumber:(NSString *)vOne withPointSeparatedVersionNumber:(NSString *)vTwo;
    @end

    @implementation SKComparator
    + (SKComparisonResult)comparePointSeparatedVersionNumber:(NSString *)vOne withPointSeparatedVersionNumber:(NSString *)vTwo {
    if (!vOne || !vTwo || ![vOne length] || ![vTwo length] || [vOne rangeOfString:@".."].location != NSNotFound ||
    [vTwo rangeOfString:@".."].location != NSNotFound) {
    return SKOrderedNotOrdered;
    }
    NSCharacterSet *numericalCharSet = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];
    NSString *vOneTrimmed = [vOne stringByTrimmingCharactersInSet:numericalCharSet];
    NSString *vTwoTrimmed = [vTwo stringByTrimmingCharactersInSet:numericalCharSet];
    if ([vOneTrimmed length] || [vTwoTrimmed length]) {
    return SKOrderedNotOrdered;
    }
    NSArray *vOneArray = [vOne componentsSeparatedByString:@"."];
    NSArray *vTwoArray = [vTwo componentsSeparatedByString:@"."];
    for (NSUInteger i = 0; i < MIN([vOneArray count], [vTwoArray count]); i++) {
    NSInteger vOneInt = [[vOneArray objectAtIndex:i] intValue];
    NSInteger vTwoInt = [[vTwoArray objectAtIndex:i] intValue];
    if (vOneInt > vTwoInt) {
    return kSKOrderedDescending;
    } else if (vOneInt < vTwoInt) {
    return kSKOrderedAscending;
    }
    }
    if ([vOneArray count] > [vTwoArray count]) {
    for (NSUInteger i = [vTwoArray count]; i < [vOneArray count]; i++) {
    if ([[vOneArray objectAtIndex:i] intValue] > 0) {
    return kSKOrderedDescending;
    }
    }
    } else if ([vOneArray count] < [vTwoArray count]) {
    for (NSUInteger i = [vOneArray count]; i < [vTwoArray count]; i++) {
    if ([[vTwoArray objectAtIndex:i] intValue] > 0) {
    return kSKOrderedAscending;
    }
    }
    }
    return kSKOrderedSame;
    }
    @end

    ReplyDelete