用程序检测Mac是否有MagSafe充电端口,或者检测USB-C端口是否存在



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

我在网上搜索了这方面的API以及shell命令,但没有找到任何好的解决方案。我需要它在通过Mac应用商店分发的沙盒环境中工作。理想情况下,任何解决方案都是纯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");
}
}

相关内容

  • 没有找到相关文章

最新更新