How to get real time battery level on iOS with a 1% granularity

后端 未结 7 1738
清酒与你
清酒与你 2020-12-05 03:19

I\'m using this function to get current battery level of device:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
UIDevice *myDevice = [UIDevice c         


        
7条回答
  •  我在风中等你
    2020-12-05 03:52

    If you are using swift 5.1 or greater this code will definitely work for you.

    Step 1:- To get started, first enable the isBatteryMonitoringEnabled property of the current device, like this:-

    UIDevice.current.isBatteryMonitoringEnabled = true
    

    Step 2:- You can now read the current battery level

    let level = UIDevice.current.batteryLevel
    print(level)
    

提交回复
热议问题