回顧一下,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相對嚴格控制,一般是獲取不到的。