iOS 设备型号大全
2017-03-23 本文已影响0人
雨燕oc
设备型号 identifier
//获得设备型号
+ (NSString *)getCurrentDeviceModel
{
int mib[2];
size_t len;
charchar *machine;
mib[0] = CTL_HW;
mib[1] = HW_MACHINE;
sysctl(mib, 2, NULL, &len, NULL, 0);
machine = malloc(len);
sysctl(mib, 2, machine, &len, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSASCIIStringEncoding];
free(machine);
// iPhone
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone2G";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone3GS";
if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone4";
if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone4";
if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone4";
if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone4S";
if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone5";
if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone5";
if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone5c";
if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone5c";
if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone5s";
if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone5s";
if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone6";
if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone6Plus";
if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone6s";
if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone6sPlus";
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";
//iPad
if ([platform isEqualToString:@"iPad1,1"]) return @"iPad";
if ([platform isEqualToString:@"iPad2,1"]) return @"iPad2";
if ([platform isEqualToString:@"iPad2,2"]) return @"iPad2";
if ([platform isEqualToString:@"iPad2,3"]) return @"iPad2";
if ([platform isEqualToString:@"iPad2,4"]) return @"iPad2";
if ([platform isEqualToString:@"iPad3,1"]) return @"iPad3";
if ([platform isEqualToString:@"iPad3,2"]) return @"iPad3";
if ([platform isEqualToString:@"iPad3,3"]) return @"iPad3";
if ([platform isEqualToString:@"iPad3,4"]) return @"iPad4";
if ([platform isEqualToString:@"iPad3,5"]) return @"iPad4";
if ([platform isEqualToString:@"iPad3,6"]) return @"iPad4";
//iPad Air
if ([platform isEqualToString:@"iPad4,1"]) return @"iPadAir";
if ([platform isEqualToString:@"iPad4,2"]) return @"iPadAir";
if ([platform isEqualToString:@"iPad4,3"]) return @"iPadAir";
if ([platform isEqualToString:@"iPad5,3"]) return @"iPadAir2";
if ([platform isEqualToString:@"iPad5,4"]) return @"iPadAir2";
//iPad mini
if ([platform isEqualToString:@"iPad2,5"]) return @"iPadmini1G";
if ([platform isEqualToString:@"iPad2,6"]) return @"iPadmini1G";
if ([platform isEqualToString:@"iPad2,7"]) return @"iPadmini1G";
if ([platform isEqualToString:@"iPad4,4"]) return @"iPadmini2";
if ([platform isEqualToString:@"iPad4,5"]) return @"iPadmini2";
if ([platform isEqualToString:@"iPad4,6"]) return @"iPadmini2";
if ([platform isEqualToString:@"iPad4,7"]) return @"iPadmini3";
if ([platform isEqualToString:@"iPad4,8"]) return @"iPadmini3";
if ([platform isEqualToString:@"iPad4,9"]) return @"iPadmini3";
if ([platform isEqualToString:@"iPad5,1"]) return @"iPadmini4";
if ([platform isEqualToString:@"iPad5,2"]) return @"iPadmini4";
if ([platform isEqualToString:@"i386"]) return @"iPhoneSimulator";
if ([platform isEqualToString:@"x86_64"]) return @"iPhoneSimulator";
return platform;
}
1.获取设备的信息
UIDevice *device = [[UIDevice alloc] int];
NSString *name = device.name; //获取设备所有者的名称
NSString *model = device.name; //获取设备的类别
NSString *type = device.localizedModel; //获取本地化版本
NSString *systemName = device.systemName; //获取当前运行的系统
NSString *systemVersion = device.systemVersion;//获取当前系统的版本
2、获取设备的唯一标示符
NSString *identifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
3、为系统创建一个随机的标示符
(NSString*) createUUID
{
NSString *id = [[NSUserDefaults standardUserDefaults] objectForKey:@"UUID"]; //获取标识为"UUID"的值
if(id == nil)
{
if([[[UIDevice currentDevice] systemVersion] floatValue] > 6.0)
{
NSString *identifierNumber = [[NSUUID UUID] UUIDString]; //ios 6.0 之后可以使用的api
[[NSUserDefaults standardUserDefaults] setObject:identifierNumber forKey:@"UUID"]; //保存为UUID
[[NSUserDefaults standardUserDefaults] synchronize];
}
else{
CFUUIDRef uuid = CFUUIDCreate(NULL);
CFStringRef uuidString = CFUUIDCreateString(NULL, uuid); //ios6.0之前使用的api
NSString *identifierNumber = [NSString stringWithFormat:@"%@", uuidString];
[[NSUserDefaults standardUserDefaults] setObject:identifierNumber forKey:@"UUID"];
[[NSUserDefaults standardUserDefaults] synchronize];
CFRelease(uuidString);
CFRelease(uuid);
}
return [[NSUserDefaults standardUserDefaults] objectForKey@"UUID"];
}
return id;
}
4、获取当前屏幕分辨率的信息
CGRect rect = [[UIScreen mainScreen] bounds];
CGFloat scale = [[UIScreen mainScreen].scale];
CGFloat width = rect.size.width * scale;
CGFloat height = rect.size.height * scale;
5、获取运营商的信息
//需要先导入头文件
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
//创建对象
CCTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
//获取运营商的名称
CTCarrier *carrier = [info subscriberCellularProvider];
NSString *mCarrier = [NSString stringWithFormat:@"%@",[carrier carrierName]];
//获取当前网络的类型
ios7之后可以按照以下方式获取。方便而且类型多
NSString *mConnectType = [[NSString alloc] initWithFormat:@"%@",info.currentRadioAccessTechnology];
//类型有以下:
CTRadioAccessTechnologyGPRS //介于2G和3G之间,也叫2.5G ,过度技术
CTRadioAccessTechnologyEdge //EDGE为GPRS到第三代移动通信的过渡,EDGE俗称2.75G
CTRadioAccessTechnologyWCDMA
CTRadioAccessTechnologyHSDPA //亦称为3.5G(3?G)
CTRadioAccessTechnologyHSUPA //3G到4G的过度技术
CTRadioAccessTechnologyCDMA1x //3G
CTRadioAccessTechnologyCDMAEVDORev0 //3G标准
CTRadioAccessTechnologyCDMAEVDORevA
CTRadioAccessTechnologyCDMAEVDORevB
CTRadioAccessTechnologyeHRPD //电信使用的一种3G到4G的演进技术, 3.75G
CTRadioAccessTechnologyLTE //接近4G
//ios7之前的话apple给我们提供了Reachability来获取。
首先要导入SystemConfiguration.framework,把下载下来的Reachability.h和Reachability.m加进项目中
Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
switch([reach currentReachabilityStatus])
{
case NotReachable: //没有连接上
//do something
break;
case ReachableViaWiFi: //通过wifi连接
//do something
break;
case ReachableViaWWAN: //通过GPRS连接
//do something
break;
default: <span style="white-space:pre"> </span>//未知情况
//do something
break;
}
6、获取当前信号的强弱
这个貌似没有给出官方的api,但是网上有人说可以用私有的api实现,但是通不过appStore的审核,方法如下:
利用linux下动态库显式调用api的函数。先包含头文件 #import <dlfcn.h>
(int) getSignalLevel
{
voidvoid *libHandle = dlopen("/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony",RTLD_LAZY);//获取库句柄
int (*CTGetSignalStrength)(); //定义一个与将要获取的函数匹配的函数指针
CTGetSignalStrength = (int(*)())dlsym(libHandle,"CTGetSignalStrength"); //获取指定名称的函数
if(CTGetSignalStrength == NULL)
return -1;
else{
int level = CTGetSignalStrength();
dlclose(libHandle); //切记关闭库
return level
}
}
7、获取电池的相关信息
@implementation BatterMonitor
//获取电池当前的状态,共有4种状态
-(NSString*) getBatteryState {
UIDevice *device = [UIDevice currentDevice];
if (device.batteryState == UIDeviceBatteryStateUnknown) {
return @"UnKnow";
}else if (device.batteryState == UIDeviceBatteryStateUnplugged){
return @"Unplugged";
}else if (device.batteryState == UIDeviceBatteryStateCharging){
return @"Charging";
}else if (device.batteryState == UIDeviceBatteryStateFull){
return @"Full";
}
return nil;
}
//获取电量的等级,0.00~1.00
-(float) getBatteryLevel {
return [UIDevice currentDevice].batteryLevel;
}
-(void) getBatteryInfo
{
NSString *state = getBatteryState();
float level = getBatteryLevel()*100.0;
//yourControlFunc(state, level); //写自己要实现的获取电量信息后怎么处理
}
//打开对电量和电池状态的监控,类似定时器的功能
-(void) didLoad
{
[[UIDevice currentDevice] setBatteryMonitoringEnable:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getBatteryInfo:) name:UIDeviceBatteryStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getBatteryInfo:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
[NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(getBatteryInfo:) userInfo:nil repeats:YES];
}
@end