如何获得Mac操作系统的实际名称而不是版本?

时间:2015-07-27 15:35:12

标签: objective-c c xcode macos cocoa

我可以使用下面给出的代码获取OSX的版本,但我想要的是操作系统的名称(使用Objective C)。

提前致谢!!!

NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];

NSString* major = [NSString stringWithFormat:@"%d", version.majorVersion];


NSString* minor = [NSString stringWithFormat:@"%d", version.minorVersion];


NSString* patch = [NSString stringWithFormat:@"%d", version.patchVersion];

3 个答案:

答案 0 :(得分:2)

我所知道的API不会产生当前操作系统版本的产品名称。对于系统位置中的产品名称,即使grep ping也会产生令人惊讶的结果,而且大多数都是在私有框架中。我发现的唯一有希望的非私人比赛是Setup Assistant.app,需要一个可怕的kludge从更长的字符串中提取:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/Localizable.strings"];
NSString *productName = [dict objectForKey:@"INSTALLATION_COMPLETE"];
if (productName) {
    NSRange r = [productName rangeOfString:@" has been"];
    if (r.location != NSNotFound) {
        productName = [productName substringToIndex:r.location];
    } else {
        productName = nil;
    }
}

这适用于Yosemite和El Capitan,并产生“OS X Yosemite”和“OS X El Capitan”。但即使有这两个版本,kludgey性质也会显现出来; El Capitan字符串包含不可破坏的空格......

除此之外(或类似的kludge使用其他不希望以这种方式使用的文件),当然可以获得数字版本并将其与已知产品名称列表匹配,这可能是我推荐的解决方案,也许将上述kludge作为后备。

答案 1 :(得分:0)

为了找出OSX名称的另一个选项,我将在下面提供解决方案:

//此函数将返回os名称

-(NSString *)getOSName{     

    // get major version 
    NSString* majorVersion = [self getMajorVersion];
    // get minor version
    NSString* minorVersion= [self getMinorVersion];

    NSString* OSName = @"";

    // figure out the name using versions
     if([minorVersion isEqualTo:@"11"])
{
    OSName = @"El Capitan";
}
else if([minorVersion isEqualTo:@"10"])
{
    OSName = @"Yosemite";
}
else if([minorVersion isEqualTo:@"9"])
{
    OSName = @"Mavericks";
}
else if([minorVersion isEqualTo:@"8"])
{
    OSName = @"Mountain Lion";
}
else if([minorVersion isEqualTo:@"7"])
{
    OSName = @"Lion";
}
else if([minorVersion isEqualTo:@"6"])
{
    OSName = @"Snow Leopard";
}
else if([minorVersion isEqualTo:@"5"])
{
    OSName = @"Leopard";
}
else if([minorVersion isEqualTo:@"4"])
{
    OSName = @"Tiger";
}
else if([minorVersion isEqualTo:@"3"])
{
    OSName = @"Panther";
}
else if([minorVersion isEqualTo:@"2"])
{
    OSName = @"Jaguar";
}
else if([minorVersion isEqualTo:@"1"])
{
    OSName = @"Puma";
}
else if([minorVersion isEqualTo:@"0"])
{
    OSName = @"Kodiak";
}



    return OSName;
}

- (的NSString *)getMajorVersion {

NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];

NSString* ver = [NSString stringWithFormat:@"%d", version.majorVersion];

return ver;

}

- (的NSString *)getMinorVersion {

NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];

NSString* ver = [NSString stringWithFormat:@"%d", version.minorVersion];

return ver;

}

答案 2 :(得分:0)

傻,但这应该可以。

public extension ProcessInfo {
func osName() -> String? {
    let version = self.operatingSystemVersion
    switch version.minorVersion {
        case 15: return "Catalina"
        case 14: return "Mojave"
        case 13: return "High Sierra"
        case 12: return "Sierra"
        case 11: return "El Capitan"
        case 10: return "Yosemite"
        case 9: return "Mavericks"
        case 8: return "Mountain Lion"
        case 7: return "Lion"
        case 6: return "Snow Leopard"
        case 5: return "Leopard"
        case 4: return "Tiger"
        case 3: return "Panther"
        case 2: return "Jaguar"
        case 1: return "Puma"
        case 0: return "Kodiak"
        default: return nil
    }
}