How to know what Mac OS the app is running on?

后端 未结 7 2003
遥遥无期
遥遥无期 2020-12-31 18:30

I\'ve seen in some projects something like:

#if .....
    code...
#endif

but i can\'t find it now...
Let\'s say, for example, if the ap

7条回答
  •  暖寄归人
    2020-12-31 19:10

    As others have said above (and I'd pick Steven Fisher's answer), you usually do not actually want to get the version number.

    And if you only need to do comparisons against a major OS X version up to the version of the current SDK you're using, NSAppKitVersionNumber (as in Monolo's answer) is the right way to do it.

    If you actually do need to get the version number for some reason (e.g., for recording analytics about your users, so you can decide when to stop supporting 10.6.0-10.6.5), here's how to do it:

    #import 
    SInt32 majorVersion, minorVersion, bugFixVersion;
    Gestalt(gestaltSystemVersionMajor, &majorVersion);
    Gestalt(gestaltSystemVersionMinor, &minorVersion);
    Gestalt(gestaltSystemVersionBugFix, &bugFixVersion);  
    

    For 10.7.3, this gives majorVersion = 10, minorVersion = 7, bugFixVersion = 3.

    The 10.7 documentation removed the paragraph that directly suggested Gestalt as the way to get OS version, but it's still not deprecated or legacy, and there's no other suggestions. In fact, every other way to get this information (parsing -[NSProcessInfo operatingSystemVersionString], calling sysctlbyname on "kern.osrelease" and converting Darwin kernel version to OS X version, etc.) is explicitly counter-indicated somewhere. So, this is the way to do it, if you really want to.

    Just keep in mind that, as the release notes for System 6.0.4 said back in 1989, this new API may not be permanent and could be removed in a future version of the OS.

提交回复
热议问题