How to define preprocessor macro to check iOS version

前端 未结 6 1631
情书的邮戳
情书的邮戳 2021-01-02 20:47

I use it to check iOS version, but it doesn\'t work:

#ifndef kCFCoreFoundationVersionNumber_iPhoneOS_5_0
#define kCFCoreFoundationVersionNumber_iPhoneOS_5_0          


        
相关标签:
6条回答
  • 2021-01-02 21:15

    For a runtime check use something like this:

    - (BOOL)iOSVersionIsAtLeast:(NSString*)version {
        NSComparisonResult result = [[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch];
        return (result == NSOrderedDescending || result == NSOrderedSame);
    }
    

    If you create a category on UIDevice for it, you can use it as such:

    @implementation UIDevice (OSVersion)
    - (BOOL)iOSVersionIsAtLeast:(NSString*)version {
        NSComparisonResult result = [[self systemVersion] compare:version options:NSNumericSearch];
        return (result == NSOrderedDescending || result == NSOrderedSame);
    }
    @end
    

    ...

    if([[UIDevice currentDevice] iOSVersionIsAtLeast:@"6.0"]) self.navigationBar.shadowImage = [UIImage new];
    
    0 讨论(0)
  • 2021-01-02 21:16
    #define isIOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] > 6.9) ?1 :0
    
    0 讨论(0)
  • 2021-01-02 21:19
    #ifdef __IPHONE_5_0 
    

    etc

    Just look for that constant. All the objective c constants start with two underscores

    0 讨论(0)
  • 2021-01-02 21:25

    You've defined a macro, but you're using it in the non-macro way. Try something like this, with your same macro definition.

    IF_IOS5_OR_GREATER(NSLog(@"iOS5");)
    

    (This is instead of your #if/#endif block.)

    0 讨论(0)
  • 2021-01-02 21:34

    Define this method:

    +(BOOL)iOS_5 {
        NSString *osVersion = @"5.0";
        NSString *currOsVersion = [[UIDevice currentDevice] systemVersion];
        return [currOsVersion compare:osVersion options:NSNumericSearch] == NSOrderedAscending;
    }
    

    Then define the macro as that method.

    0 讨论(0)
  • 2021-01-02 21:39

    Much simpler:

    #define IS_IOS6_AND_UP ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
    
    0 讨论(0)
提交回复
热议问题