How do I know if an iOS device is connected via USB or a wall charger?

I want to know if there is any possibility in iOS through which we can find out if the device is connected via a USB cable or Wallport. in other words, I want my application to display whether the device is charging via a USB cable or a port on the wall.

+4
source share
2 answers

From UIDevice Class Reference

UIDeviceBatteryStateDidChangeNotification Posted when battery state changes. For this notification to be sent, you must set the batteryMonitoringEnabled property to YES. You can obtain the battery state by getting the value of the batteryState property. 

When you receive a notification, you need to check

 UIDeviceBatteryState == UIDeviceBatteryStateUnplugged 

Listing

 UIDeviceBatteryState The battery power state of the device. typedef enum { UIDeviceBatteryStateUnknown, UIDeviceBatteryStateUnplugged, UIDeviceBatteryStateCharging, UIDeviceBatteryStateFull, } UIDeviceBatteryState; 
+3
source

It is simply impossible - and there is probably no good reason to rely on it.

Think of the many different types of computers that are turned on and off, with software installed or missing, users logging in or out of the system, and hundreds of different models of chargers, USB hubs, and even USB screens.

0
source

All Articles