欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS 获取手机设备信息

程序员文章站 2022-04-20 08:34:54
回顾一下,Android中获取手机设备相关信息:通过android.os.Build、context.getResources().getDisplayMetrics()、Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID)、(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)等等,从而获取到手机设备:手机型...

回顾一下,Android中获取手机设备相关信息:通过android.os.Build、context.getResources().getDisplayMetrics()、Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID)、(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)等等,从而获取到手机设备:手机型号、手机品牌、设备名、手机系统、屏幕宽高等等一系列信息,其中还涉及到权限问题。

相对IOS,则是从开发库中获取设备相关信息:通过UIDevice、NSLocale、UIScreen、CTTelephonyNetworkInfo等等,从而获取到手机设备:设备名称、系统名称、系统版本、屏幕宽高等等一系列信息,但需要通过指定的开发库才能获取得到。

此处需要导入部分开发库:

#import <sys/utsname.h>
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <SystemConfiguration/CaptiveNetwork.h>
#import <NetworkExtension/NetworkExtension.h>

DeviceInfo.h

#import <UIKit/UIKit.h>
#import <sys/utsname.h>
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <SystemConfiguration/CaptiveNetwork.h>
#import <NetworkExtension/NetworkExtension.h>
@class DeviceInfo;

#define DUMMY_MAC_ADDR  @"02:00:00:00:00:00"

/**
 * 设备相关信息
 **/
@interface DeviceInfo : NSObject

/**
 * 设备名称
 **/
@property (nonatomic, strong) NSString *deviceName;

/**
 * 系统名称
 **/
@property (nonatomic, strong) NSString *systemName;

/**
 * 系统版本
 **/
@property (nonatomic, strong) NSString *systemVersion;

/**
 * 设备模式
 **/
@property (nonatomic, strong) NSString *model;

/**
 * 本地设备模式
 **/
@property (nonatomic, strong) NSString *localizedModel;

/**
 * 系统语言
 **/
@property (nonatomic, strong) NSString *language;

/**
 * 国家
 **/
@property (nonatomic, strong) NSString *country;

/**
 * IDFV
 **/
@property (nonatomic, strong) NSString *deviceIDFV;

/**
 * 屏幕宽度
 **/
@property (nonatomic) int width;

/**
 * 屏幕高度
 **/
@property (nonatomic) int height;

/**
 * 屏幕宽度分辨率
 **/
@property (nonatomic) int screenWidth;

/**
 * 屏幕高度分辨率
 **/
@property (nonatomic) int screenHeight;

/**
 * 运营商
 **/
@property (nonatomic, strong) NSString *carrierName;

/**
 * mac
 **/
@property (nonatomic, strong) NSString *mac;

@end

DeviceInfo.m

#import <Foundation/Foundation.h>
#import "DeviceInfo.h"

@implementation DeviceInfo

- (NSString *)deviceName{
    NSString *dName = [[UIDevice currentDevice] name];
    if (dName) {
        return dName;
    }
    return @"";
}

- (NSString *)systemName{
    NSString *sName = [[UIDevice currentDevice] systemName];
    if (sName) {
        return sName;
    }
    return @"";
}

- (NSString *)systemVersion{
    NSString *sVersion = [[UIDevice currentDevice] systemVersion];
    if (sVersion) {
        return sVersion;
    }
    return @"";
}

- (NSString *)model{
//    NSString *mod = [[UIDevice currentDevice] model];
//    if (mod) {
//        return mod;
//    }
    
    struct utsname systemInfo;
    uname(&systemInfo);
    
    NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
    
    if (!deviceModel) {
        return @"";
    }
    
    if ([deviceModel isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([deviceModel isEqualToString:@"iPhone3,2"])    return @"iPhone 4";
    if ([deviceModel isEqualToString:@"iPhone3,3"])    return @"iPhone 4";
    if ([deviceModel isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([deviceModel isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
    if ([deviceModel isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
    if ([deviceModel isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
    if ([deviceModel isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
    if ([deviceModel isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
    if ([deviceModel isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
    if ([deviceModel isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([deviceModel isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    if ([deviceModel isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    if ([deviceModel isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    if ([deviceModel isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
    // 日行两款手机型号均为日本独占,可能使用索尼FeliCa支付方案而不是苹果支付
    if ([deviceModel isEqualToString:@"iPhone9,1"])    return @"iPhone 7";
    if ([deviceModel isEqualToString:@"iPhone9,2"])    return @"iPhone 7 Plus";
    if ([deviceModel isEqualToString:@"iPhone9,3"])    return @"iPhone 7";
    if ([deviceModel isEqualToString:@"iPhone9,4"])    return @"iPhone 7 Plus";
    if ([deviceModel isEqualToString:@"iPhone10,1"])   return @"iPhone_8";
    if ([deviceModel isEqualToString:@"iPhone10,4"])   return @"iPhone_8";
    if ([deviceModel isEqualToString:@"iPhone10,2"])   return @"iPhone_8_Plus";
    if ([deviceModel isEqualToString:@"iPhone10,5"])   return @"iPhone_8_Plus";
    if ([deviceModel isEqualToString:@"iPhone10,3"])   return @"iPhone X";
    if ([deviceModel isEqualToString:@"iPhone10,6"])   return @"iPhone X";
    if ([deviceModel isEqualToString:@"iPhone11,8"])   return @"iPhone XR";
    if ([deviceModel isEqualToString:@"iPhone11,2"])   return @"iPhone XS";
    if ([deviceModel isEqualToString:@"iPhone11,6"])   return @"iPhone XS Max";
    if ([deviceModel isEqualToString:@"iPhone11,4"])   return @"iPhone XS Max";
    if ([deviceModel isEqualToString:@"iPhone12,1"])   return @"iPhone 11";
    if ([deviceModel isEqualToString:@"iPhone12,3"])   return @"iPhone 11 Pro";
    if ([deviceModel isEqualToString:@"iPhone12,5"])   return @"iPhone 11 Pro Max";
    if ([deviceModel isEqualToString:@"iPhone12,8"])   return @"iPhone SE2";

    if ([deviceModel isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([deviceModel isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([deviceModel isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([deviceModel isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([deviceModel isEqualToString:@"iPod5,1"])      return @"iPod Touch (5 Gen)";
    if ([deviceModel isEqualToString:@"iPod7,1"])      return @"iPod Touch (6 Gen)";
    if ([deviceModel isEqualToString:@"iPod9,1"])      return @"iPod Touch (7 Gen)";
    if ([deviceModel isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([deviceModel isEqualToString:@"iPad1,2"])      return @"iPad 3G";
    if ([deviceModel isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([deviceModel isEqualToString:@"iPad2,2"])      return @"iPad 2";
    if ([deviceModel isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([deviceModel isEqualToString:@"iPad2,4"])      return @"iPad 2";
    if ([deviceModel isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([deviceModel isEqualToString:@"iPad2,6"])      return @"iPad Mini";
    if ([deviceModel isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([deviceModel isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([deviceModel isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([deviceModel isEqualToString:@"iPad3,3"])      return @"iPad 3";
    if ([deviceModel isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([deviceModel isEqualToString:@"iPad3,5"])      return @"iPad 4";
    if ([deviceModel isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
    if ([deviceModel isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
    if ([deviceModel isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
    if ([deviceModel isEqualToString:@"iPad4,4"])      return @"iPad Mini 2 (WiFi)";
    if ([deviceModel isEqualToString:@"iPad4,5"])      return @"iPad Mini 2 (Cellular)";
    if ([deviceModel isEqualToString:@"iPad4,6"])      return @"iPad Mini 2";
    if ([deviceModel isEqualToString:@"iPad4,7"])      return @"iPad Mini 3";
    if ([deviceModel isEqualToString:@"iPad4,8"])      return @"iPad Mini 3";
    if ([deviceModel isEqualToString:@"iPad4,9"])      return @"iPad Mini 3";
    if ([deviceModel isEqualToString:@"iPad5,1"])      return @"iPad Mini 4 (WiFi)";
    if ([deviceModel isEqualToString:@"iPad5,2"])      return @"iPad Mini 4 (LTE)";
    if ([deviceModel isEqualToString:@"iPad5,3"])      return @"iPad Air 2";
    if ([deviceModel isEqualToString:@"iPad5,4"])      return @"iPad Air 2";
    if ([deviceModel isEqualToString:@"iPad6,3"])      return @"iPad Pro 9.7";
    if ([deviceModel isEqualToString:@"iPad6,4"])      return @"iPad Pro 9.7";
    if ([deviceModel isEqualToString:@"iPad6,7"])      return @"iPad Pro 12.9";
    if ([deviceModel isEqualToString:@"iPad6,8"])      return @"iPad Pro 12.9";
    if ([deviceModel isEqualToString:@"iPad6,11"])      return @"iPad (5 Gen)";
    if ([deviceModel isEqualToString:@"iPad6,12"])      return @"iPad (5 Gen)";
    if ([deviceModel isEqualToString:@"iPad7,5"])      return @"iPad (6 Gen)";
    if ([deviceModel isEqualToString:@"iPad7,6"])      return @"iPad (6 Gen)";
    if ([deviceModel isEqualToString:@"iPad7,11"])      return @"iPad (7 Gen)";
    if ([deviceModel isEqualToString:@"iPad7,12"])      return @"iPad (7 Gen)";
    if ([deviceModel isEqualToString:@"iPad7,1"])      return @"iPad Pro 12.9 (2 Gen)";
    if ([deviceModel isEqualToString:@"iPad7,2"])      return @"iPad Pro 12.9 (2 Gen)";
    if ([deviceModel isEqualToString:@"iPad7,3"])      return @"iPad Pro 10.5";
    if ([deviceModel isEqualToString:@"iPad7,4"])      return @"iPad Pro 10.5";
    if ([deviceModel isEqualToString:@"iPad8,1"])      return @"iPad Pro 11";
    if ([deviceModel isEqualToString:@"iPad8,2"])      return @"iPad Pro 11";
    if ([deviceModel isEqualToString:@"iPad8,3"])      return @"iPad Pro 11";
    if ([deviceModel isEqualToString:@"iPad8,4"])      return @"iPad Pro 11";
    if ([deviceModel isEqualToString:@"iPad8.5"])      return @"iPad Pro 12.9 (3 Gen)";
    if ([deviceModel isEqualToString:@"iPad8.6"])      return @"iPad Pro 12.9 (3 Gen)";
    if ([deviceModel isEqualToString:@"iPad8.7"])      return @"iPad Pro 12.9 (3 Gen)";
    if ([deviceModel isEqualToString:@"iPad8.8"])      return @"iPad Pro 12.9 (3 Gen)";
    if ([deviceModel isEqualToString:@"iPad8.9"])      return @"iPad Pro 11 (2 Gen)";
    if ([deviceModel isEqualToString:@"iPad8.10"])      return @"iPad Pro 11 (2 Gen)";
    if ([deviceModel isEqualToString:@"iPad8.11"])      return @"iPad Pro 12.9 (4 Gen)";
    if ([deviceModel isEqualToString:@"iPad8.12"])      return @"iPad Pro 12.9 (4 Gen)";
    if ([deviceModel isEqualToString:@"iPad11,1"])      return @"iPad Mini (5 Gen)";
    if ([deviceModel isEqualToString:@"iPad11,2"])      return @"iPad Mini (5 Gen)";
    if ([deviceModel isEqualToString:@"iPad11,3"])      return @"iPad Air (3 Gen)";
    if ([deviceModel isEqualToString:@"iPad11,4"])      return @"iPad Air (3 Gen)";
    if ([deviceModel isEqualToString:@"iPad11,6"])      return @"iPad (8 Gen)";
    if ([deviceModel isEqualToString:@"iPad11,7"])      return @"iPad (8 Gen)";
    if ([deviceModel isEqualToString:@"iPad13,1"])      return @"iPad Air (4 Gen)";
    if ([deviceModel isEqualToString:@"iPad13,2"])      return @"iPad Air (4 Gen)";

    if ([deviceModel isEqualToString:@"Watch1,1"])      return @"Apple Watch 1";
    if ([deviceModel isEqualToString:@"Watch1,2"])      return @"Apple Watch 1";
    if ([deviceModel isEqualToString:@"Watch2,3"])      return @"Apple Watch Series 2";
    if ([deviceModel isEqualToString:@"Watch2,4"])      return @"Apple Watch Series 2";
    if ([deviceModel isEqualToString:@"Watch2,6"])      return @"Apple Watch Series 1";
    if ([deviceModel isEqualToString:@"Watch2,7"])      return @"Apple Watch Series 1";
    if ([deviceModel isEqualToString:@"Watch3,1"])      return @"Apple Watch Series 3";
    if ([deviceModel isEqualToString:@"Watch3,2"])      return @"Apple Watch Series 3";
    if ([deviceModel isEqualToString:@"Watch3,3"])      return @"Apple Watch Series 3";
    if ([deviceModel isEqualToString:@"Watch3,4"])      return @"Apple Watch Series 3";
    if ([deviceModel isEqualToString:@"Watch4,1"])      return @"Apple Watch Series 4";
    if ([deviceModel isEqualToString:@"Watch4,2"])      return @"Apple Watch Series 4";
    if ([deviceModel isEqualToString:@"Watch4,3"])      return @"Apple Watch Series 4";
    if ([deviceModel isEqualToString:@"Watch4,4"])      return @"Apple Watch Series 4";
    if ([deviceModel isEqualToString:@"Watch5,1"])      return @"Apple Watch Series 5";
    if ([deviceModel isEqualToString:@"Watch5,2"])      return @"Apple Watch Series 5";
    if ([deviceModel isEqualToString:@"Watch5,3"])      return @"Apple Watch Series 5";
    if ([deviceModel isEqualToString:@"Watch5,4"])      return @"Apple Watch Series 5";
    if ([deviceModel isEqualToString:@"Watch5,9"])      return @"Apple Watch SE";
    if ([deviceModel isEqualToString:@"Watch5,10"])      return @"Apple Watch SE";
    if ([deviceModel isEqualToString:@"Watch5,11"])      return @"Apple Watch SE";
    if ([deviceModel isEqualToString:@"Watch5,12"])      return @"Apple Watch SE";
    if ([deviceModel isEqualToString:@"Watch6,1"])      return @"Apple Watch Series 6";
    if ([deviceModel isEqualToString:@"Watch6,2"])      return @"Apple Watch Series 6";
    if ([deviceModel isEqualToString:@"Watch6,3"])      return @"Apple Watch Series 6";
    if ([deviceModel isEqualToString:@"Watch6,4"])      return @"Apple Watch Series 6";

    if ([deviceModel isEqualToString:@"AppleTV1,1"])      return @"Apple TV 1";
    if ([deviceModel isEqualToString:@"AppleTV2,1"])      return @"Apple TV 2";
    if ([deviceModel isEqualToString:@"AppleTV3,1"])      return @"Apple TV 3";
    if ([deviceModel isEqualToString:@"AppleTV3,2"])      return @"Apple TV 3";
    if ([deviceModel isEqualToString:@"AppleTV5,3"])      return @"Apple TV 4";
    if ([deviceModel isEqualToString:@"AppleTV6,2"])      return @"Apple TV 4K";

    if ([deviceModel isEqualToString:@"AirPods1,1"])      return @"AirPods 1";
    if ([deviceModel isEqualToString:@"AirPods2,1"])      return @"AirPods 2";
    if ([deviceModel isEqualToString:@"iProd8,1"])      return @"AirPods Pro";

    if ([deviceModel isEqualToString:@"AudioAccessory1,1"])      return @"HomePod";
    if ([deviceModel isEqualToString:@"AudioAccessory1,2"])      return @"HomePod";

    if ([deviceModel isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceModel isEqualToString:@"x86_64"])       return @"Simulator";
    return deviceModel;
}

- (NSString *)localizedModel{
    NSString *localModel = [[UIDevice currentDevice] localizedModel];
    if (localModel) {
        return localModel;
    }
    return @"";
}

- (NSString *)language{
    NSArray *languageArray = [NSLocale preferredLanguages];
    NSString *language = [languageArray objectAtIndex:0];
    if (language) {
        return language;
    }
    return @"";
}

- (NSString *)country{
    NSLocale *locale = [NSLocale currentLocale];
    NSString *country = [locale localeIdentifier];
    if (country) {
        return country;
    }
    return @"";
}

- (NSString *)deviceIDFV{
    NSUUID *idfvUUID = [[UIDevice currentDevice] identifierForVendor];
    if (idfvUUID) {
        NSString *idfv = [idfvUUID UUIDString];
        if (idfv) {
            return idfv;
        }
    }
    return @"";
}

- (int)width{
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    CGFloat width = size.width;
    int widthInt = [[NSNumber numberWithFloat:width] intValue];
    if (widthInt) {
        return widthInt;
    }
    return 0;
}

- (int)height{
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    CGFloat height = size.height;
    int heightInt = [[NSNumber numberWithFloat:height] intValue];
    if (heightInt) {
        return heightInt;
    }
    return 0;
}

- (int)screenWidth{
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    CGFloat scale_screen = [UIScreen mainScreen].scale;
    int screenWidthInt = [[NSNumber numberWithFloat:size.width * scale_screen] intValue];
    if (screenWidthInt) {
        return screenWidthInt;
    }
    return 0;
}

- (int)screenHeight{
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    CGFloat scale_screen = [UIScreen mainScreen].scale;
    int screenHeightInt = [[NSNumber numberWithFloat:size.height * scale_screen] intValue];
    if (screenHeightInt) {
        return screenHeightInt;
    }
    return 0;
}

- (NSString *)carrierName{
    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    CTCarrier *carrier = info.subscriberCellularProvider;
    if(carrier.carrierName) {
        return carrier.carrierName;
    }
    return @"";
}

- (NSString *)mac{
    NSArray*wfMac=CFBridgingRelease(CNCopySupportedInterfaces());
    id info1 = nil;
    for (NSString *macName in wfMac) {
        info1 = (__bridge_transfer id)CNCopyCurrentNetworkInfo((CFStringRef) macName);
        if (info1 && [info1 count]) {
            break;
        }
    }
    NSDictionary *dic = (NSDictionary *)info1;
    NSString *wifiMac = [dic objectForKey:@"BSSID"];
    if(wifiMac) {
        return wifiMac;
    }
    return DUMMY_MAC_ADDR;
}

@end

注:获取设备model中systemInfo.machine设备信息获取是相对较为完善且全面的,若后续有新的设备新增,请从 Models - The iPhone Wiki 上进行续补;Apple对于获取MAC相对严格控制,一般是获取不到的。

本文地址:https://blog.csdn.net/github_36262442/article/details/108827871