How to check Mac OS X version at runtime

前端 未结 5 1255
孤街浪徒
孤街浪徒 2021-01-12 22:48

I am using below code to check OS X version at runtime.

if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_10)
{
    /* On a 10.10.x or earlier s         


        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 23:34

    If you need to support multiple systems (you can adapt method return value):

    #import 
    
    + (NSString *)systemVersion
    {
      static NSString *systemVersion = nil;
    
      if (!systemVersion) {
        typedef struct {
          NSInteger majorVersion;
          NSInteger minorVersion;
          NSInteger patchVersion;
        } MyOperatingSystemVersion;
    
        if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)]) {
          MyOperatingSystemVersion version = ((MyOperatingSystemVersion(*)(id, SEL))objc_msgSend_stret)([NSProcessInfo processInfo], @selector(operatingSystemVersion));
          systemVersion = [NSString stringWithFormat:@"Mac OS X %ld.%ld.%ld", (long)version.majorVersion, version.minorVersion, version.patchVersion];
        }
        else {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
          SInt32 versMaj, versMin, versBugFix;
          Gestalt(gestaltSystemVersionMajor, &versMaj);
          Gestalt(gestaltSystemVersionMinor, &versMin);
          Gestalt(gestaltSystemVersionBugFix, &versBugFix);
          systemVersion = [NSString stringWithFormat:@"Mac OS X %d.%d.%d", versMaj, versMin, versBugFix];
    #pragma clang diagnostic pop
        }
      }
    
      return systemVersion;
    }
    

提交回复
热议问题