博客> iOS 获取设备信息
iOS 获取设备信息
2019-12-10 06:45 评论:0 阅读:1069 老白爱学习
ios device

由于最近工作需要整理了一下获取设备信息相关的方法

1.获取设备型号 (模拟器,真机)

需要导入头文件 sys/sysctl.h和sys/utsname.h

- (DeviceType)getDeviceModel {

struct utsname systemInfo;

uname(&systemInfo);

DeviceType deviceTypeModel=DEVICE_TYPE_UNKNOWN;

NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

if ([deviceString isEqualToString:@"i386"]){
    deviceTypeModel = DEVICE_TYPE_SIMULATOR;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"x86_64"]){
    deviceTypeModel = DEVICE_TYPE_SIMULATOR;
    return deviceTypeModel;
}

//判断iPhone

if ([deviceString isEqualToString:@"iPhone9,4"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_7_PLUS;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone9,3"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_7;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone9,2"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_7_PLUS;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone9,1"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_7;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone8,4"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_SE;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone8,2"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_6S_PLUS;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone8,1"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_6S;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone7,2"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_6;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone7,1"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_6_PLUS;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone6,2"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_5S_GSM_CDMA;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone6,1"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_5S_GSM;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone5,4"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_5C_GSM_CDMA;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone5,3"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_5C_GSM;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone5,2"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_5_GSM_CDMA;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone5,1"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_5;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone4,1"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_4S;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone3,3"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_4;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone3,2"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_4;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPhone3,1"]){
    deviceTypeModel = DEVICE_TYPE_IPHONE_4;
    return deviceTypeModel;
}

//判断iPod Touch

if ([deviceString isEqualToString:@"iPod5,1"]){
    deviceTypeModel = DEVICE_TYPE_IPOD_TOUCH_1G;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPod4,1"]){
    deviceTypeModel = DEVICE_TYPE_IPOD_TOUCH_1G;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPod3,1"]){
    deviceTypeModel = DEVICE_TYPE_IPOD_TOUCH_1G;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPod2,1"]){
    deviceTypeModel = DEVICE_TYPE_IPOD_TOUCH_1G;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPod1,1"]){
    deviceTypeModel = DEVICE_TYPE_IPOD_TOUCH_1G;
    return deviceTypeModel;
}

//判断iPad

if ([deviceString isEqualToString:@"iPad6,8"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_PRO_12_9;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad6,7"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_PRO_12_9;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad6,4"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_PRO_9_7;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad6,3"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_PRO_9_7;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad5,4"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_AIR_2;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad5,3"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_AIR_2;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad5,2"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_MINI_4_LTE;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad5,1"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_MINI_4_WIFI;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad4,9"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_MINI_3;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad4,8"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_MINI_3;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad4,7"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_MINI_3;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad4,6"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_MINI_2;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad4,5"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_AIR_CELLULAR;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad4,4"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_MINI_2_WIFI;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad4,2"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_AIR_CELLULAR;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad4,1"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_AIR_WIFI;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad3,6"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_4_GSM_CDMA;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad3,5"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_4;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad3,4"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_4_WIFI;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad3,3"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_3;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad3,2"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_3_GSM_CDMA;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad3,1"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_3_WIFI;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad2,7"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_MINI_GSM_CDMA;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad2,6"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_MINI;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad2,5"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_MINI_WIFI;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad2,4"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_2;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad2,3"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_2_CDMA;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad2,2"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_2;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad2,1"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_2_WIFI;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad1,2"]){
    deviceTypeModel = DEVICE_TYPE_IPAD_3G;
    return deviceTypeModel;
}

if ([deviceString isEqualToString:@"iPad1,1"]){
    deviceTypeModel = DEVICE_TYPE_IPAD;
    return deviceTypeModel;
}

return deviceTypeModel;

}

2.获取设备名称

- (NSString *)getDeviceName {

NSString *deviceName = [[UIDevice currentDevice] name];

return deviceName;

}

3.获取设备系统名称

- (NSString *)getDeviceSystemName {

NSString *deviceSysName = [[UIDevice currentDevice] systemName];

return deviceSysName;

}

4.获取设备系统版本号

DeviceSysyemVersion自定义版本号枚举

- (DeviceSysyemVersion)getDeviceSysVersion {

NSString *deviceSysVersionStr = [[UIDevice currentDevice] systemVersion];

DeviceSysyemVersion deviceSysVersion = IOS_VERSION_UNKNOWN;//初始化版本

if ([deviceSysVersionStr isEqualToString:@"10.2"]) {
    deviceSysVersion = IOS_VERSION_10_2;
    return deviceSysVersion;
}

if ([deviceSysVersionStr isEqualToString:@"10.1"]) {
    deviceSysVersion = IOS_VERSION_10_1;
    return deviceSysVersion;
}

if ([deviceSysVersionStr isEqualToString:@"10.0"]) {
    deviceSysVersion = IOS_VERSION_10_0;
    return deviceSysVersion;
}

if ([deviceSysVersionStr isEqualToString:@"9.3"]) {
    deviceSysVersion = IOS_VERSION_9_3;
    return deviceSysVersion;
}

if ([deviceSysVersionStr isEqualToString:@"9.2"]) {
    deviceSysVersion = IOS_VERSION_9_2;
    return deviceSysVersion;
}

if ([deviceSysVersionStr isEqualToString:@"9.1"]) {
    deviceSysVersion = IOS_VERSION_9_1;
    return deviceSysVersion;
}

if ([deviceSysVersionStr isEqualToString:@"9.0"]) {
    deviceSysVersion = IOS_VERSION_9_0;
    return deviceSysVersion;
}

if ([deviceSysVersionStr isEqualToString:@"8.4"]) {
    deviceSysVersion = IOS_VERSION_8_4;
    return deviceSysVersion;
}

if ([deviceSysVersionStr isEqualToString:@"8.3"]) {
    deviceSysVersion = IOS_VERSION_8_3;
    return deviceSysVersion;
}

if ([deviceSysVersionStr isEqualToString:@"8.2"]) {
    deviceSysVersion = IOS_VERSION_8_2;
    return deviceSysVersion;
}

if ([deviceSysVersionStr isEqualToString:@"8.1"]) {
    deviceSysVersion = IOS_VERSION_8_1;
    return deviceSysVersion;
}

if ([deviceSysVersionStr isEqualToString:@"8.0"]) {
    deviceSysVersion = IOS_VERSION_8_0;
    return deviceSysVersion;
}

if ([deviceSysVersionStr compare:@"8.0"] == NSOrderedAscending) {//版本小于8.0
    deviceSysVersion = IOS_VERSION_TOO_LOW_TO_SUPPORT;
    return deviceSysVersion;
}

return deviceSysVersion;

}

5.获取设备的唯一标识符

- (NSString *)getDeviceUUID {

NSString *deviceUUID = [[[UIDevice currentDevice]identifierForVendor] UUIDString];

return deviceUUID;

}

6.获取当前设备总容量大小

- (NSNumber *)getDeviceTotalDiskSpace {

//获取NSHomeDirectory()中的内容集合
NSDictionary *fattributes = [[NSFileManager defaultManager]attributesOfFileSystemForPath:NSHomeDirectory() error:nil];

//取到设备总存储大小
NSNumber *sumSize = [fattributes objectForKey:NSFileSystemSize];

return sumSize;

}

7.获取当前设备可用容量大小

- (NSNumber *)getDeviceFreeDiskSpace {

//获取NSHomeDirectory()中的内容集合
NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
//取到设备空闲存储大小
NSNumber *freeSize = [fattributes objectForKey:NSFileSystemFreeSize];

return freeSize; }

8.获取当前设备可用运行内存

- (unsigned long)getDeviceAvailableMemory {

//初始化结构体vm_statistics_data_t
vm_statistics_data_t vmStats;

//创建查询任务

mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;

// 调用查询内存任务
kern_return_t kernReturn = host_statistics(mach_host_self(),
                                           HOST_VM_INFO,
                                           (host_info_t)&vmStats,
                                           &infoCount);

if (kernReturn != KERN_SUCCESS) { return NSNotFound; }

return (vm_page_size *vmStats.free_count) ;

}

9.获取设备品屏幕尺寸

- (CGSize)getDeviceSize {

//屏幕尺寸
CGRect rect = [[UIScreen mainScreen] bounds];

CGSize size = rect.size;

return size;

}

10.获取设备的Scale 像素

- (CGFloat)getDeviceScale {

CGFloat scale = [UIScreen mainScreen].scale;

return scale;

}

11.获取设备运营商和运营商编号

 需要导入头文件CoreTelephony/CTCarrier.h

 获取设备运营商

- (NSString *)getDeviceCarrierName {

//初始化CTT对象
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];

//获取运营商信息

CTCarrier *carrier = info.subscriberCellularProvider;

//获取运营商信息名称
NSString *carrierName = carrier.carrierName;

return carrierName;

}

 获取设备运营商编号

- (NSString *)getDeviceCarrierNameIMSI {

//初始化CTT对象
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];

//获取运营商信息
CTCarrier *carrier = info.subscriberCellularProvider;

//取到运营商MCC编号
NSString *mcc = [carrier mobileCountryCode];

//取到运营商MNC编号
NSString *mnc = [carrier mobileNetworkCode];

NSString *imsi = [NSString stringWithFormat:@"%@%@", mcc, mnc];

return imsi;

}

12.获取设备当前的网络

 需要导入头文件CoreTelephony/CTTelephonyNetworkInfo.h
 DeviceNetworkType自定义网络类型枚举

- (DeviceNetworkType)getDeviceNetworkType {

//获取application对象

UIApplication *application = [UIApplication sharedApplication];

//获取statusBar和foregroundView保存信息的数组
NSArray *children = [[[application valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];

//获取到网络返回码
int type = 0;

for (id child in children) {

   if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {

        type = [[child valueForKeyPath:@"dataNetworkType"] intValue];

    }

}

DeviceNetworkType networkType;//初始化枚举
switch (type) {
    case NETWORKTYPE_NULL:
        networkType = deviceNetworkTypeNull;//无网模式
        break;
    case NETWORKTYPE_2G:
        networkType = deviceNetworkType2G;//2G模式
        break;
    case NETWORKTYPE_3G:
        networkType = deviceNetworkType3G;//3G模式
        break;
    case NETWORKTYPE_4G:
        networkType = deviceNetworkType4G;//4G模式
        break;
    case NETWORKTYPE_LTE:
        networkType = deviceNetworkTypeLTE;//LTE网络
        break;
    case NETWORKTYPE_WIFI:
        networkType = deviceNetworkTypeWIFI;//Wife模式
        break;
    default:
        networkType = deviceNetworkTypeUnknown;//不识别的网络
        break;
}
return networkType;//返回网络类型

}

13.判断设备当前GPS状态

 需要导入头文件CoreLocation/CoreLocation.h

- (BOOL)deviceIsOpenGPS {

//定位管理器的locationServicesEnabled
if([CLLocationManager locationServicesEnabled]) {

    return YES;//开启

}

return NO;//未开启

}

14.获取设备电池的充电信息

- (DeviceBatteryStateType)getDeviceBatterState {

//通过UIDevice获取电池的状态转换。
UIDevice *device = [UIDevice currentDevice];

device.batteryMonitoringEnabled = YES;//开启了监视电池状态的功能

DeviceBatteryStateType batteryStateType;

//switch判断类型
switch (device.batteryState) {
    case UIDeviceBatteryStateUnknown:
        batteryStateType = DeviceBatteryStateTypeUnknown;//无法获得充电状态
        break;
    case UIDeviceBatteryStateUnplugged:
        batteryStateType = DeviceBatteryStateTypeUnplugged;//非充电状态
        break;
    case UIDeviceBatteryStateCharging:
        batteryStateType = DeviceBatteryStateTypeCharging;//充电状态
        break;
    case UIDeviceBatteryStateFull:
        batteryStateType = DeviceBatteryStateTypeFull;//充满状态
        break;

    default:
        batteryStateType = DeviceBatteryStateTypeUnusual;//其他异常状态
        break;
}

return batteryStateType;

}

15.获取设备电池电量信息

- (float)getDeviceBatterLevel {

//通过UIDevice获取电池的状态转换。
UIDevice *device = [UIDevice currentDevice];

device.batteryMonitoringEnabled = YES;//开启了监视电池状态的功能

return device.batteryLevel;

}

16.获取设备Wifi信号强度(在Wifi状态下返回字符串类型的1,2,3...)

- (NSString *)getDeviceWifiSignalStrength {

//获取application对象
UIApplication *app = [UIApplication sharedApplication];

//获取statusBar和foregroundView保存信息的数组
NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];

NSString *dataNetworkItemView = nil;

//遍历
for (id subview in subviews) {//获取网络返回码
    if ([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
        dataNetworkItemView = subview;
        break;
    }
}

return [dataNetworkItemView valueForKey:@"_wifiStrengthBars"];//取到wifi状态时对应的信号强度的字符串

}

17.判断设备耳机是否插入

 需要导入头文件AVFoundation/AVFoundation.h

- (BOOL)isDeviceHeadphonesPluggedIn {

AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];//获取设备当前音频信息

for (AVAudioSessionPortDescription* desc in [route outputs]) {//遍历设备输出信号的音频信息找到端口描述这个类

    if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones]){//如果端口的类型是耳机

        return YES;

}

}

return NO;

}

18.判断蓝牙状态 获取蓝牙的状态需要在使用的.h文件中导入头文件然后遵守代理 在初始化一个设备管理者CBCentralManager,如下图  无标题.png 然后在对应的.m文件中初始化成员变量,实现代理方法即可,如下图  123.png

排版搞了半天实在是整不好看了,但是不影响阅读,以后改进吧!

收藏
1
sina weixin mail 回到顶部