After iOS13 update, the test partner feedback video playback, in landscape mode can not see the status bar information!! Until some time ago, WHEN I watched a live App, I found that the status bar showed the time and battery power in the horizontal screen. I thought it was restored. However, there is no such thing. If you look at the status bar of some mainstream video playing apps, you will find that they are basically different from each other. But they all have something in common: time + batteries.
The author also made a simple:
time
The first is time, generally mobile phones have two hours: 12H and 24H system. There is a slight difference in the display between the two, such as 3pm, 12H: 3pm, and 24H: 15:00, you need to judge whether the current time is 12H or 24H. This is simply to print the current time, check whether it contains AM or PM. In 12H system: 3:00 PM, in 24H system: 15:00. Time system of judgment:
- (BOOL)is24H{ if(! _is24H){ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setLocale:[NSLocale currentLocale]]; [formatter setDateStyle:NSDateFormatterNoStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; NSString *dateString = [formatter stringFromDate:[NSDate date]]; NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]]; NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]]; _is24H = (amRange.location == NSNotFound && pmRange.location == NSNotFound); } return _is24H; }Copy the code
Add this notification if you also want to listen to the user switch timing in the phone’s system Settings
UIKIT_EXTERN NSString *const UIApplicationSignificantTimeChangeNotification;
Copy the code
You can write a timer to update the current time information, but there is a trick: In the case of landscape screen, the top toolbar will automatically hide animation after displaying for a period of time (this time may only be a few seconds), and the user will manually trigger the display again. Therefore, it only needs to update the time information when the top toolbar needs to be displayed, so as to avoid the timer occupying memory. A listed live streaming App did just that.
The battery
The battery has two information, one is the amount of power, and one is the state of the battery: whether the battery is in a low state (less than or equal to 20%); Whether low battery mode is enabled (red battery, yellow battery); Whether the battery is being charged (the battery is green and the normal charging icon of the lightning bolt is displayed).
We can obtain the battery status through batteryState and obtain the battery quantity information through batteryLevel. It is worth noting that batteryMonitoringEnabled needs to be manually set to YES before obtaining battery information, otherwise BatteryState will return to Unkonw.
[UIDevice currentDevice].batteryMonitoringEnabled = YES; NSLog(@" Battery: %.2f", [UIDevice currentDevice].batteryLevel); NSLog(@" Battery status = %ld",[UIDevice currentDevice].batteryState);Copy the code
The battery has the following states:
UIDeviceBatteryStateUnknown / / / unknown UIDeviceBatteryStateUnplugged / / / not charging UIDeviceBatteryStateCharging / / / Charging (less than 100%) UIDeviceBatteryStateFull // Battery is full (not removed)Copy the code
- Note: The battery count may be 1% different from what the system shows. It’s actually very difficult to get accurate data, at least not yet, including the fact that I used runtime to read private variables in the status bar. In order to avoid this problem, station B and some large factories simply do not display the specific percentage value of the battery, but only display the electricity chart of the corresponding proportion.
In addition, we also need to determine whether the user is in low-power mode:
// Enable low power mode NSLog(@" enable low power mode = %d",[NSProcessInfo processInfo].lowPowerModeEnabled);Copy the code
We can also use notifications to monitor battery levels and changes in battery status
UIKIT_EXTERN NSString *const UIDeviceBatteryLevelDidChangeNotification; / / / battery change UIKIT_EXTERN nsstrings * const UIDeviceBatteryStateDidChangeNotification; /// The battery status changesCopy the code
Among them, to send the notice, must be isBatteryMonitoringEnabled attribute set to true. Notifications of battery level changes should not be sent more than once per minute. Do not attempt to calculate battery discharge rate or battery remaining time; Discharge rates may change frequently depending on the built-in application and your application.
Here are two things to note when detecting low battery mode: Low battery mode only supports iOS 9. When an App needs to be compatible with older versions, check the availability of the API first. Low battery mode only works on iPhone. On iPad, isLowPowerModeEnabled always returns NO. When we detect that a user has turned on low battery mode, we can make some small changes to help extend the user’s battery life. Here are some tips from Apple:
- Stop using the location service
- Attenuate animation
- Stop background tasks (such as network requests)
- Disable motion tracking
Finally, I wrote a simple demo: RCStatusBar