iOS gets the current device model and other information. of includes iPhone7 and iPhone7P

  • 2020-12-26 05:53:51
  • OfStack


#include <sys/types.h> 
#include <sys/sysctl.h>
// Acquired equipment model  
+ (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; 
}

Get a summary of device information

1. Obtain the information of the device


UIDevice *device = [[UIDevice alloc] int]; 
NSString *name = device.name; // Gets the name of the device owner  
NSString *model = device.name; // Gets the category of the device  
NSString *type = device.localizedModel; // Get the localized version  
NSString *systemName = device.systemName; // Gets the currently running system  
NSString *systemVersion = device.systemVersion;// Gets the version of the current system 

2. Obtain the only identifier of the device


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

3. Create a random identifier for the system


(NSString*) createUUID 
{ 
NSString *id = [[NSUserDefaults standardUserDefaults] objectForKey:@"UUID"]; // Get the identity as "UUID" The value of the  
if(id == nil) 
{ 
if([[[UIDevice currentDevice] systemVersion] floatValue] > 6.0) 
{ 
NSString *identifierNumber = [[NSUUID UUID] UUIDString]; //ios 6.0  It can be used later api 
[[NSUserDefaults standardUserDefaults] setObject:identifierNumber forKey:@"UUID"]; // Save as UUID 
[[NSUserDefaults standardUserDefaults] synchronize]; 
} 
else{ 
CFUUIDRef uuid = CFUUIDCreate(NULL); 
CFStringRef uuidString = CFUUIDCreateString(NULL, uuid); //ios6.0 Previously used 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. Get information about the current screen resolution


CGRect rect = [[UIScreen mainScreen] bounds]; 
CGFloat scale = [[UIScreen mainScreen].scale]; 
CGFloat width = rect.size.width * scale; 
CGFloat height = rect.size.height * scale;

5. Access to operator information

A pilot header file is required


#import <CoreTelephony/CTCarrier.h> 
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

Create an object


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

Gets the name of the operator


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

Gets the type of the current network

ios7 can be obtained after this as follows. Convenient and of many types


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

The types are as follows:


CTRadioAccessTechnologyGPRS // between 2G and 3G Between, also called 2.5G , Excessive technology  
CTRadioAccessTechnologyEdge //EDGE for GPRS To the first 3 The transition of generation mobile communications, EDGE Commonly known as 2.75G 
CTRadioAccessTechnologyWCDMA 
CTRadioAccessTechnologyHSDPA // Also called 3.5G(3?G) 
CTRadioAccessTechnologyHSUPA //3G to 4G The transition technique of  
CTRadioAccessTechnologyCDMA1x //3G 
CTRadioAccessTechnologyCDMAEVDORev0 //3G standard  
CTRadioAccessTechnologyCDMAEVDORevA 
CTRadioAccessTechnologyCDMAEVDORevB 
CTRadioAccessTechnologyeHRPD // Telecommunication used 1 Kind of 3G to 4G Evolutionary technology,  3.75G 
CTRadioAccessTechnologyLTE // Close to the 4G

Before ios7, apple gave us Reachability to get.

First, import SystemConfiguration.framework and add the downloaded ES45en.h and Reachability.m to your project


UIDevice *device = [[UIDevice alloc] int]; 
NSString *name = device.name; // Gets the name of the device owner  
NSString *model = device.name; // Gets the category of the device  
NSString *type = device.localizedModel; // Get the localized version  
NSString *systemName = device.systemName; // Gets the currently running system  
NSString *systemVersion = device.systemVersion;// Gets the version of the current system 
0

6. Obtain the current signal strength

This does not seem to give the official api, but some people on the Internet said that it could be implemented with private api, but it could not pass the appStore audit, the method is as follows:

Using linux dynamic library to explicitly call api functions. Include the header file #import first < dlfcn.h >


UIDevice *device = [[UIDevice alloc] int]; 
NSString *name = device.name; // Gets the name of the device owner  
NSString *model = device.name; // Gets the category of the device  
NSString *type = device.localizedModel; // Get the localized version  
NSString *systemName = device.systemName; // Gets the currently running system  
NSString *systemVersion = device.systemVersion;// Gets the version of the current system 
1

7. Equipment vibration

You need to add AudioToolbox framework and import header #import < AudioToolbox/AudioToolbox.h >

Add code where vibrations are needed:


UIDevice *device = [[UIDevice alloc] int]; 
NSString *name = device.name; // Gets the name of the device owner  
NSString *model = device.name; // Gets the category of the device  
NSString *type = device.localizedModel; // Get the localized version  
NSString *systemName = device.systemName; // Gets the currently running system  
NSString *systemVersion = device.systemVersion;// Gets the version of the current system 
2

But this doesn't seem to support incoming vibration times or modes, so you can control them yourself.

8. Get battery information


@implementation BatterMonitor 
// Gets the current state of the battery, total 4 Kind of state  
-(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; 
} 
// To get the power level, 0.00~1.00 
-(float) getBatteryLevel { 
return [UIDevice currentDevice].batteryLevel; 
} 

-(void) getBatteryInfo 
{ 
NSString *state = getBatteryState(); 
float level = getBatteryLevel()*100.0; 
//yourControlFunc(state, level); // How to deal with the information of getting electricity after writing oneself to implement  
} 
// Turn on the monitoring of power and battery status, similar to the function of timer  
-(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. Open a web page in app

This one is simple and can be used directly with the provided interface openURL.


UIDevice *device = [[UIDevice alloc] int]; 
NSString *name = device.name; // Gets the name of the device owner  
NSString *model = device.name; // Gets the category of the device  
NSString *type = device.localizedModel; // Get the localized version  
NSString *systemName = device.systemName; // Gets the currently running system  
NSString *systemVersion = device.systemVersion;// Gets the version of the current system 
4

10. Open another app in app

Opening another app can still be done via openURL. But there are two cases. The first is to launch built-in apps, like phone, browser, SMS and

Mail can be called directly and add parameters, such as


UIDevice *device = [[UIDevice alloc] int]; 
NSString *name = device.name; // Gets the name of the device owner  
NSString *model = device.name; // Gets the category of the device  
NSString *type = device.localizedModel; // Get the localized version  
NSString *systemName = device.systemName; // Gets the currently running system  
NSString *systemVersion = device.systemVersion;// Gets the version of the current system 
5

In the second case, you want to open your own app. In this case, you want to register one URL for the app you are going to open. This can be registered in the project file info.plist.

The main operations are:

Step1. Right click and select Add Row

Step2. Key Value Select "URL types"

3. Open "Item 0" and add 1 URL identifier to the key. It can be anything, but it is recommended to use an "anti-domain name" (for example, "ES130en.fcplayer.testHello").

Step4. Add one more line under Item 0.

Step5. Select "URL Schemes" as Key.

Enter your URL protocol name (e.g. "testHello://" should be written as "testHello"). If necessary, you can add multiple protocols here.

URL Schemes, URL identifier is optional. If you need to pass parameters, you can add your parameters on URL Schemes:// in the same format as web development. (or URL Schemes://URL identifier@ added parameter) the key is to define the processing method with the receiving party. Then add the code where you want to open it:


UIDevice *device = [[UIDevice alloc] int]; 
NSString *name = device.name; // Gets the name of the device owner  
NSString *model = device.name; // Gets the category of the device  
NSString *type = device.localizedModel; // Get the localized version  
NSString *systemName = device.systemName; // Gets the currently running system  
NSString *systemVersion = device.systemVersion;// Gets the version of the current system 
6

Related articles: