sln device - eju-front/mobile-solution GitHub Wiki

获取设备信息

目的

实现获取 Android 和 iOS 平台的设备信息

安装指南

安装 device 插件,详细使用文档见 cordova-plugin-device

cordova plugin add cordova-plugin-device

使用方法

按照以下代码编写即可获得设备信息

console.log(device);

执行以下语句后,控制台会输出以下数据

​Android

{
    cordova: "5.2.2",
    isVirtual: false,
    manufacturer: "samsung",
    model: "GT-I9500",
    platform: "Android",
    serial: "4d0054f557af60d1",
    uuid: "5e57659aa946c7b3",
    version: "5.0.1"
}

​iOS

{
    cordova: "5.2.2",
    isVirtual: true,
    manufacturer: "Apple",
    model: "x86_64",
    platform: "iOS",
    serial: "4d0054f557af60d1",
    uuid: "61D0D668-D479-4A28-B542-71F184A36D87",
    version: "9.2"
}

以上 Android 的设备 ID 返回的是 Android 设备 ID 研究 里的方案四的方式

iOS原生获取设备信息

UIDevice *device = [[UIDevice alloc] int];  
NSString *name = device.name;                    //获取设备所有者的名称  
NSString *model = device.model;                  //获取设备的类别  
NSString *type = device.localizedModel;          //获取本地化版本  
NSString *systemName = device.systemName;        //获取当前运行的系统  
NSString *systemVersion = device.systemVersion;  //获取当前系统的版本 
NSString *identifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];  //设备的唯一标示

// 获取当前屏幕分辨率的信息
CGRect rect = [[UIScreen mainScreen] bounds];  
CGFloat scale = [[UIScreen mainScreen].scale];  
CGFloat width = rect.size.width * scale;  
CGFloat height = rect.size.height * scale;

设备型号

大部分 Android 的设备型号的对照表都可以从 Google 官网获得,有 CSV 和 PDF 两种格式

iOS的设备型号

+ (NSString *)devicePlatForm{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
 
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4 (A1332)";
    if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4 (A1332)";
    if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4 (A1349)";
    if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S (A1387/A1431)";
    if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5 (A1428)";
    if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5 (A1429/A1442)";
    if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c (A1456/A1532)";
    if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c (A1507/A1516/A1526/A1529)";
    if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s (A1453/A1533)";
    if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s (A1457/A1518/A1528/A1530)";
    if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus (A1522/A1524)";
    if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6 (A1549/A1586)";
    if ([platform isEqualToString:@"iPhone8,3"]) return @"iPhoneSE";  
    if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhoneSE";  
    if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone7";  
    if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone7Plus";
}