如何以编程方式确定正在运行的Mac OS X版本?

时间:2021-04-11 06:59:03

I have a program which needs to behave slightly differently on Tiger than on Leopard. Does anybody know of a system call which will allow me to accurately determine which version of Mac OS X I am running. I have found a number of macro definitions to determine the OS of the build machine, but nothing really good to determine the OS of the running machine.

我有一个程序需要在Tiger上的行为略微不同于Leopard。有没有人知道系统调用,这将允许我准确地确定我正在运行的Mac OS X版本。我已经找到了许多宏定义来确定构建机器的操作系统,但是没有什么能够确定正在运行的机器的操作系统。

Thanks, Joe

8 个解决方案

#1


15  

See this article here

在这里看到这篇文章

But in short, if you're using carbon, use the Gestalt() call, and if you're using cocoa, there is a constant called NSAppKitVersionNumber which you can simply check against.

但简而言之,如果您使用的是碳,请使用Gestalt()调用,如果您使用的是cocoa,则会有一个名为NSAppKitVersionNumber的常量,您只需检查它即可。

Edit: For Mac OSX 10.8 and above, don't use Gestalt() anymore. See this answer for more details: How do I determine the OS version at runtime in OS X or iOS (without using Gestalt)?

编辑:对于Mac OSX 10.8及更高版本,请不要再使用Gestalt()。有关更多详细信息,请参阅此答案:如何在OS X或iOS(不使用Gestalt)中确定运行时的操作系统版本?

#2


12  

Could you just check for the presence of a capability? For instance:

你能检查一下能力的存在吗?例如:

if (NSClassFromString(@"NSKeyedArchiver") != Nil)

or

if ([arrayController respondsToSelector: @selector(selectedIndexes)])

then you know that the operating system does what you need it to do, not that Apple's product marketing group gave it a particular number ;-)

然后你知道操作系统做你需要它做的事情,而不是Apple的产品营销组给它一个特定的数字;-)

#3


8  

The API is through the Gestalt Manager.

API通过格式塔管理器。

See "Determining OS Version" in the CocoaDev site.

请参阅CocoaDev站点中的“确定操作系统版本”。

#4


6  

In terminal:

system_profiler SPSoftwareDataType

Gives:

Software:

    System Software Overview:

      System Version: Mac OS X 10.5.5 (9F33)
      Kernel Version: Darwin 9.5.0
      Boot Volume: Main
      Boot Mode: Normal
      Computer Name: phoenix
      User Name: Douglas F Shearer (dougal)
      Time since boot: 2 days 16:55

Or:

sw_vers

Gives:

ProductName:    Mac OS X
ProductVersion: 10.5.5
BuildVersion:   9F33

#5


2  

Is the OS version really what you want? There may be a more appropriate thing to test for, such as the presence of, or version number of, a particular framework.

OS版本真的是你想要的吗?可能有更合适的测试方法,例如特定框架的存在或版本号。

#6


1  

within your program you can use Gestalt. Here is the code I am using for my program to obtain the OS version.

在你的程序中,你可以使用格式塔。这是我用于获取操作系统版本的程序的代码。

long version = 0;
OSStatus rc0 = Gestalt(gestaltSystemVersion, &version);
if((rc0 == 0) && (version >= 0x1039)) {      
    // will work with version 10.3.9
    // works best with version 10.4.9
    return; // version is good
}
if(rc0) {
    printf("gestalt rc=%i\n", (int)rc0);
} else {
    printf("gestalt version=%08x\n", version);
}

#7


0  

respondsToSelector: almost certainly is better than you maintaining a table of what given releases do and do not implement.

respondsToSelector:几乎肯定比维护给定版本执行和不执行的表格更好。

Be lazy. Let the runtime tell you whether it can do something or not, and fall back to older methods when you need to. Your code will be far less fragile because you don't have to maintain your own global data that the rest of your code has to keep checking with.

偷懒。让运行时告诉您它是否可以执行某些操作,并在需要时回退到旧方法。您的代码将不那么脆弱,因为您不必维护自己的全局数据,其余代码必须继续检查。

#8


0  

Run this in the command line:

在命令行中运行:

system_profiler SPSoftwareDataType | grep Mac

#1


15  

See this article here

在这里看到这篇文章

But in short, if you're using carbon, use the Gestalt() call, and if you're using cocoa, there is a constant called NSAppKitVersionNumber which you can simply check against.

但简而言之,如果您使用的是碳,请使用Gestalt()调用,如果您使用的是cocoa,则会有一个名为NSAppKitVersionNumber的常量,您只需检查它即可。

Edit: For Mac OSX 10.8 and above, don't use Gestalt() anymore. See this answer for more details: How do I determine the OS version at runtime in OS X or iOS (without using Gestalt)?

编辑:对于Mac OSX 10.8及更高版本,请不要再使用Gestalt()。有关更多详细信息,请参阅此答案:如何在OS X或iOS(不使用Gestalt)中确定运行时的操作系统版本?

#2


12  

Could you just check for the presence of a capability? For instance:

你能检查一下能力的存在吗?例如:

if (NSClassFromString(@"NSKeyedArchiver") != Nil)

or

if ([arrayController respondsToSelector: @selector(selectedIndexes)])

then you know that the operating system does what you need it to do, not that Apple's product marketing group gave it a particular number ;-)

然后你知道操作系统做你需要它做的事情,而不是Apple的产品营销组给它一个特定的数字;-)

#3


8  

The API is through the Gestalt Manager.

API通过格式塔管理器。

See "Determining OS Version" in the CocoaDev site.

请参阅CocoaDev站点中的“确定操作系统版本”。

#4


6  

In terminal:

system_profiler SPSoftwareDataType

Gives:

Software:

    System Software Overview:

      System Version: Mac OS X 10.5.5 (9F33)
      Kernel Version: Darwin 9.5.0
      Boot Volume: Main
      Boot Mode: Normal
      Computer Name: phoenix
      User Name: Douglas F Shearer (dougal)
      Time since boot: 2 days 16:55

Or:

sw_vers

Gives:

ProductName:    Mac OS X
ProductVersion: 10.5.5
BuildVersion:   9F33

#5


2  

Is the OS version really what you want? There may be a more appropriate thing to test for, such as the presence of, or version number of, a particular framework.

OS版本真的是你想要的吗?可能有更合适的测试方法,例如特定框架的存在或版本号。

#6


1  

within your program you can use Gestalt. Here is the code I am using for my program to obtain the OS version.

在你的程序中,你可以使用格式塔。这是我用于获取操作系统版本的程序的代码。

long version = 0;
OSStatus rc0 = Gestalt(gestaltSystemVersion, &version);
if((rc0 == 0) && (version >= 0x1039)) {      
    // will work with version 10.3.9
    // works best with version 10.4.9
    return; // version is good
}
if(rc0) {
    printf("gestalt rc=%i\n", (int)rc0);
} else {
    printf("gestalt version=%08x\n", version);
}

#7


0  

respondsToSelector: almost certainly is better than you maintaining a table of what given releases do and do not implement.

respondsToSelector:几乎肯定比维护给定版本执行和不执行的表格更好。

Be lazy. Let the runtime tell you whether it can do something or not, and fall back to older methods when you need to. Your code will be far less fragile because you don't have to maintain your own global data that the rest of your code has to keep checking with.

偷懒。让运行时告诉您它是否可以执行某些操作,并在需要时回退到旧方法。您的代码将不那么脆弱,因为您不必维护自己的全局数据,其余代码必须继续检查。

#8


0  

Run this in the command line:

在命令行中运行:

system_profiler SPSoftwareDataType | grep Mac