iOS:以编程方式检测Lightning与30针端口

时间:2014-08-26 12:29:58

标签: ios

我需要检测iOS设备是否配备Lightning端口或老式30针端口。

最可靠的方法是什么?

1 个答案:

答案 0 :(得分:1)

使用它来获取设备模型字符串: iOS - How to get device make and model?

然后进行手动启发式决定使用的端口。这假设未来的iOS设备将具有Lightning端口,并且机器名称中的数字将遵循与现在(2014年8月)相同的模型

NSString *machineName()
{
    struct utsname systemInfo;
    uname(&systemInfo);

    return [NSString stringWithCString:systemInfo.machine
                              encoding:NSUTF8StringEncoding];
}


NS_ENUM(NSUInteger, MachineConnectorType)
{
    MachineConnectorTypeUnknown,
    MachineConnectorType30Pin,
    MachineConnectorTypeLightning
};

enum MachineConnectorType MachineConnectorTypeWithMachineName(NSString *machineName)
{
    if([machineName rangeOfString:@"iPad"].location != NSNotFound)
    {
        // 1st gen mini wants to be special
        if([machineName isEqualToString:@"iPad2,5"])
        {
            return MachineConnectorTypeLightning;
        }

        NSString *model = [machineName substringFromIndex:4];
        if(model.intValue >= 3)
            return MachineConnectorTypeLightning;
        return MachineConnectorType30Pin;
    }
    else if ([machineName rangeOfString:@"iPod"].location != NSNotFound)
    {
        NSString *model = [machineName substringFromIndex:4];
        if(model.intValue >= 5)
            return MachineConnectorTypeLightning;
        return MachineConnectorType30Pin;
    }
    else if ([machineName rangeOfString:@"iPhone"].location != NSNotFound)
    {
        NSString *model = [machineName substringFromIndex:6];
        if(model.intValue >= 5)
            return MachineConnectorTypeLightning;
        return MachineConnectorType30Pin;
    }

    return MachineConnectorTypeUnknown;
}