以编程方式检测 Mac 是否具有 MagSafe 充电端口,或检测 USB-C 端口是否存在

Detect Whether Mac Has MagSafe Charging Port Programmatically, Or Detect If USB-C Port Exists

我正在尝试找出一种方法来以编程方式确定 Mac 是否具有 MagSafe 充电端口。如果那不可能,或者,我想检测是否存在任何 USB-C 端口(因此 MagSafe 不存在)。

我在网上搜索了此 API 以及 shell 命令,但没有找到任何好的解决方案。我需要它在通过 Mac App Store 分发的沙盒环境中工作。理想情况下,任何解决方案都是纯粹的 Cocoa/Objective-C,但我也可以通过 NSTask 或类似的方式实现这一点。

非常感谢您提出任何建议!

我最终从 EveryMac.com 的所有带有 MagSafe 的型号中编制了一个列表:

- (bool) macHasMagSafe
{
    NSString *macModel = @"";

    size_t len = 0;
    sysctlbyname("hw.model", NULL, &len, NULL, 0);

    if (len)
    {
        char *model = malloc(len*sizeof(char));
        sysctlbyname("hw.model", model, &len, NULL, 0);
        macModel = [NSString stringWithUTF8String:model];
        free(model);
    }
    else
    {
        macModel = @"UNKNOWN";
    }
    
    NSArray *magSafeModels = [[NSArray alloc] initWithObjects:
                              
                              // MACBOOK MODELS
                              @"MacBook7,1",
                              @"MacBook6,1",
                              @"MacBook5,2",
                              @"MacBook5,1",
                              @"MacBook4,1",
                              @"MacBook3,1",
                              @"MacBook2,1",
                              @"MacBook1,1",
                              
                              // MACBOOK PRO MODELS
                              @"MacBookPro11,5",
                              @"MacBookPro11,4",
                              @"MacBookPro12,1",
                              @"MacBookPro11,3",
                              @"MacBookPro11,2",
                              @"MacBookPro11,1",
                              @"MacBookPro10,1",
                              @"MacBookPro10,2",
                              @"MacBookPro9,1",
                              @"MacBookPro9,2",
                              @"MacBookPro8,3",
                              @"MacBookPro8,2",
                              @"MacBookPro8,1",
                              @"MacBookPro6,1",
                              @"MacBookPro6,2",
                              @"MacBookPro7,1",
                              @"MacBookPro5,2",
                              @"MacBookPro5,3",
                              @"MacBookPro5,4",
                              @"MacBookPro5,5",
                              @"MacBookPro5,1",
                              @"MacBookPro4,1",
                              @"MacBookPro3,1",
                              @"MacBookPro2,1",
                              @"MacBookPro2,2",
                              @"MacBookPro1,2",
                              @"MacBookPro1,1",
                              
                              // MACBOOK AIR MODELS
                              @"MacBookAir7,2",
                              @"MacBookAir7,1",
                              @"MacBookAir6,2",
                              @"MacBookAir6,1",
                              @"MacBookAir5,2",
                              @"MacBookAir5,1",
                              @"MacBookAir4,2",
                              @"MacBookAir4,1",
                              @"MacBookAir3,2",
                              @"MacBookAir3,1",
                              @"MacBookAir2,1",
                              @"MacBookAir1,1",
                              
                              nil];
    
    if ([magSafeModels containsObject:macModel])
    {
        return YES;
        //NSLog(@"This Mac has a MagSafe port");
    }
    else
    {
        return NO;
        //NSLog(@"This Mac DOES NOT have a MagSafe port");
    }
}