[Programmer Diary] See UIDevice again

Posted by heepofajeep on Sun, 07 Jul 2019 00:21:20 +0200

The last article introduced UIApplication This is the class that controls the application. This article looks at UIDevice, which is easily overlooked but often needs to be dealt with.

As the name implies, it is equipment. I believe that in our daily development, we will be more or less exposed to some of the things, such as video applications need screen rotation, feedback will increase the system version, system information and so on. The following contents are mostly from the api documents, and I hope to teach more.

1. Getting device information

Open UIDevice and you will see the following attributes.

@property(nonatomic,readonly,strong) NSString    *name;              // e.g. "My iPhone"
@property(nonatomic,readonly,strong) NSString    *model;             // e.g. @"iPhone", @"iPod touch"
@property(nonatomic,readonly,strong) NSString    *localizedModel;    // localized version of model
@property(nonatomic,readonly,strong) NSString    *systemName;        // e.g. @"iOS"
@property(nonatomic,readonly,strong) NSString    *systemVersion;     // e.g. @"4.0"

[Supplement]
1. Judging that the device model can use UI_USER_INTERFACE_IDIOM() in addition to the model, it is actually a UIUser Interface Idiom with several values:

typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) {
  UIUserInterfaceIdiomUnspecified = -1,
  UIUserInterfaceIdiomPhone , // iPhone and iPod touch style UI
  UIUserInterfaceIdiomPad , // iPad style UI
  UIUserInterfaceIdiomTV , // Apple TV style UI
  UIUserInterfaceIdiomCarPlay , // CarPlay style UI
};

2. Get device name using sysctlbyname(),sysctl() standard unix function

- (NSString *) platformString
{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = malloc(size);
    sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithUTF8String:machine];
    free(machine);
    if ([platform isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    //............
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    return @"";
}

3. The above information is often used in feedback, but at the same time it will increase some application information. Another important tool, NSBundle, is used. Although I mainly talk about UIDevice, I would like to introduce all the relevant.

Using NSBundle, we can access the files under the application through relative paths, which brings convenience to development.

However, in NSBundle, app is used to obtain relevant information:

NSDictionary *dicInfo = [[NSBundle mainBundle] infoDictionary];

2. Equipment rotation

Notification when monitoring equipment rotates
UIDeviceOrientationDidChangeNotification

Detect whether the direction of the current device has changed and the YES direction has changed.

@property(nonatomic,readonly,getter=isGeneratingDeviceOrientationNotifications) BOOL generatesDeviceOrientationNotifications ;

Start to change the direction of the device. If you need to deal with something while changing the direction, you can rewrite this method.

- (void)beginGeneratingDeviceOrientationNotifications;

End with changing the direction of the device. If you need to deal with something when changing the direction, you can rewrite this method. Ibid.

- (void)endGeneratingDeviceOrientationNotifications ;

UIDevice Orientation has the following enumeration types:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
} __TVOS_PROHIBITED;

For the above types, it is supplemented in the following note.

[note]
This is the problem I encountered in the project: in making live broadcast function module, using the function of the bullet screen (the bullet screen uses the third-party library Barrage Renderer), the bullet screen opens automatically for the horizontal screen, and closes when the vertical screen. In the test, it was found that the screen sometimes turned on or off, and finally it was found that it could not be displayed when the screen was placed horizontally. The reason is that there is an error in the setting method.

Judging the horizontal and vertical screen from the state of [UIDevice current Device]. orientation will cause errors, because its enumeration type is UIDevice Orientation. When you look at its enumeration type, you will find that besides Portrait Landscape Left Landscape Right Portrait Upside Down, there are two states of FaceUp FaceDown, ignoring its settings will produce an error. Impacts.

UIDevice Orientation Enumeration

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
} __TVOS_PROHIBITED;

It can also be converted into UIInterface Orientation when used, because the latter has only a few common types.

UIDeviceOrientation orientation             = [UIDevice currentDevice].orientation;
UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)orientation;

UIInterface Orientation Enumeration

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;

In addition, we will monitor the status of the screen when switching between horizontal and vertical screens. Usually we will monitor UIDevice Orientation Did Change Notification. The result is UIDevice Orientation. We can also monitor UI Application Did Change StatusBar Frame Notification to get UIInterface Orientation. Of course, UI Application DidChange StatusBarFrameNotification can also monitor issues that arise in hot spots and other events.

3. Access to Electricity

@property(nonatomic,getter=isBatteryMonitoringEnabled) BOOL batteryMonitoringEnabled   // Information about battery remaining and charging status can be obtained by default of NO.
@property(nonatomic,readonly) UIDeviceBatteryState          batteryState   // Charging status
@property(nonatomic,readonly) float                         batteryLevel   // 0.0-1.0 Suitable for failure is -1.0

Note:
1. BateryLevel returns a percentage of 0-1.0 if
2. UIDevice BatteryState has several States

typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
  UIDeviceBatteryStateUnknown, Failure to obtain charging status
  UIDeviceBatteryStateUnplugged,   // Non-charging state
  UIDeviceBatteryStateCharging,    // Charging status (not full)
  UIDeviceBatteryStateFull,        // A state of fullness (fullness)
}

There are two notifications when monitoring electricity:

UIDeviceBatteryStateDidChangeNotification   //Monitoring Battery Residual Electricity 
UIDeviceBatteryLevelDidChangeNotification   //Monitor charging status

See an article about using IOKit or instrument to get the power of equipment. Amway gives you:
Obtaining power information for IOS devices: Battery Level

4. Proximity sensor

Here is the sensor of the device. When the proximity sensor is enabled, it detects whether there is a large object in front of it. If there is one, it closes the screen and gives a general notification. When the obstacle is removed, the screen is reopened. This prevents the wrong use of the ear to touch the button in the notification process.

@property(nonatomic,getter=isProximityMonitoringEnabled) BOOL proximityMonitoringEnabled //Proximity sensing can be obtained by default of NO
@property(nonatomic,readonly)                            BOOL proximityState   // YES proximity message trigger

There are two notifications when the listening message is triggered:
UIDeviceProximityStateDidChangeNotification

for example

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityChanged:) name:UIDeviceProximityStateDidChangeNotificatio object:device];}


- (void) proximityChanged:(NSNotification *)notification {

  UIDevice *device = [notification object];

  if(device.proximityState==1){
  //do something
  }
}

5. Multitask Processing

Support multi-task processing

@property(nonatomic,readonly,getter=isMultitaskingSupported) BOOL multitaskingSupported ;

More content follow-up supplements, hope to teach more.

Topics: iOS less Unix simulator