iOS获取当前设备型号等信息(全)包含iPhone7和iPhone7P

时间:2021-07-18 20:23:03
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <sys/types.h>
#include <sys/sysctl.h>
//获得设备型号
+ (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";
//iPod Touch
if ([platform isEqualToString:@"iPod1,1"]) return @"iPodTouch";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPodTouch2G";
if ([platform isEqualToString:@"iPod3,1"]) return @"iPodTouch3G";
if ([platform isEqualToString:@"iPod4,1"]) return @"iPodTouch4G";
if ([platform isEqualToString:@"iPod5,1"]) return @"iPodTouch5G";
if ([platform isEqualToString:@"iPod7,1"]) return @"iPodTouch6G";
//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、获取设备的信息

?
1
2
3
4
5
6
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、获取设备的唯一标示符

?
1
NSString *identifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

3、为系统创建一个随机的标示符

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(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、获取当前屏幕分辨率的信息

?
1
2
3
4
CGRect rect = [[UIScreen mainScreen] bounds];
CGFloat scale = [[UIScreen mainScreen].scale];
CGFloat width = rect.size.width * scale;
CGFloat height = rect.size.height * scale;

5、获取运营商的信息

需要先导入头文件

?
1
2
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

创建对象

?
1
CCTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];

获取运行商的名称

?
1
2
CTCarrier *carrier = [info subscriberCellularProvider];
NSString *mCarrier = [NSString stringWithFormat:@"%@",[carrier carrierName]];

获取当前网络的类型

ios7之后可以按照以下方式获取。方便而且类型多

?
1
NSString *mConnectType = [[NSString alloc] initWithFormat:@"%@",info.currentRadioAccessTechnology];

类型有以下:

?
1
2
3
4
5
6
7
8
9
10
11
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加进项目中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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>

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
(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、设备震动

需要加入AudioToolbox framework,导入头文件 #import <AudioToolbox/AudioToolbox.h>

在需要震动的地方添加代码:

?
1
AudioServicesPlaySystemSound ( kSystemSoundID_Vibrate) ;

但是貌似这个不支持传入震动时间和模式,自己去控制吧。

8、获取电池的相关信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@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

9、app中打开一个网页

这个比较简单,直接用提供的接口openURL即可。

?
1
2
NSString *url = @"www.apple.com"
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

10、app中打开另一个app

打开另一个app还是可以通过openURL来实现。但是要分两种情况。第一种是启动内置的应用,一般的电话,浏览器,短信和

邮件可以直接调用并添加参数,譬如

?
1
2
3
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://10086"]];

第二种情况是要打开自己开发的app,这种情况则要为将要打开的app注册一个URL协议。这个可以在项目的文件info.plist中注册。

主要操作为:

Step1. 右键,选择“Add Row”

Step2. Key值选择“URL types”

Step3. 打开“Item 0″,然后为该key增加一个URL identifier。可以是任何值,但建议用“反域名”(例如 “com.fcplayer.testHello”)。

Step4. 在“Item 0”下再加一行。

Step5. 选择“URL Schemes” 作为Key。

Step6. 输入你的URL协议名 (例如“testHello://” 应写做“testHello”)。如果有必要,你可以在这里加入多个协议。

其实在打开的时候只需要URL Schemes即可,URL identifier是可选项。如果需要传送参数,可以在URL Schemes://添加你的参数,格式和网页开发的传递参数差不多。(又或者URL Schemes://URL identifier@添加的参数)关键是要和接收参数方定义好处理的方式。然后在需要打开的地方添加代码:

?
1
2
NSString *url = @"URL Schemes的路径"
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

以上所述是小编给大家介绍的iOS获取当前设备型号等信息包含iPhone7和iPhone7P,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!