1

Hi I am new in IOS development and I am using swift. I want to get battery level of my device programmatically. But always I receive -1.0

Below is my code -

import UIKit

class FirstController: UIViewController{

       override func viewDidLoad() {

        super.viewDidLoad()

        UIDevice.current.isBatteryMonitoringEnabled = true

    }


    var batteyLevel : Float {

        return UIDevice.current.batteryLevel

    }


    // attached with a touchInsideUp event
    @IBAction func onButtonClick(_ sender: Any) {

    // print "Battery moniter true"
        print("Battery moniter \(UIDevice.current.isBatteryMonitoringEnabled)")

       // print "Battery -1.0"
        print("Battery \(batteyLevel)")
    }

}

Please help how to find battery level

Sanjay Kumar
  • 1,135
  • 14
  • 27
  • Check if `isBatteryMonitoringEnabled` is **really** true in `batteryLevel` computed variable. – user28434'mstep Jan 22 '18 at 15:02
  • 1
    From `batteryLevel`: "If battery monitoring is not enabled, battery state is UIDeviceBatteryStateUnknown and the value of this property is –1.0." What is the battery state? – Larme Jan 22 '18 at 15:04
  • 1
    Make sure you are using a real device to test it – Leo Dabus Jan 22 '18 at 15:05
  • @ user28434 Hi, I have check it is true – Sanjay Kumar Jan 22 '18 at 15:06
  • Are you testing it on simulator? – Leo Dabus Jan 22 '18 at 15:07
  • @LeoDabus , yes i am on real device – Sanjay Kumar Jan 22 '18 at 15:07
  • @SanjayMajoka it doesn't make any sense. Post your actual code – Leo Dabus Jan 22 '18 at 15:07
  • `func onButtonClick(){ print("Battery : \(batteryLevel)") }` will never be called on a button click. You should ask how to create a button (IBAction) – Leo Dabus Jan 22 '18 at 15:11
  • @LeoDabus, why not? He could add it using `button.addTarget(…)` – user28434'mstep Jan 22 '18 at 15:14
  • @user28434 Do you really think OP have done that? – Leo Dabus Jan 22 '18 at 15:15
  • @LeoDabus I have created a touchUpInside event and i am able to print. After printing i have put my Question. Above is simply defined code... In viewDidLoad(), A varibale batteryLevel and a click event to call onButtonClick()... I guess problem is something different. Hi device battery percentage is 100%... I thing i should receive 1.0.. Please provide some info – Sanjay Kumar Jan 22 '18 at 15:15
  • @SanjayMajoka edit your question. Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. https://stackoverflow.com/help/mcve – Leo Dabus Jan 22 '18 at 15:18
  • @LeoDabus ok i will update it – Sanjay Kumar Jan 22 '18 at 15:20
  • Try to isolating your problem in a sample project and post the link – Leo Dabus Jan 22 '18 at 15:21
  • @LeoDabus i have wrote my code above you can check – Sanjay Kumar Jan 22 '18 at 15:30
  • 1
    I am pretty sure you are not testing it on a real device. I can't help you. Your code should work if you were testing it on the device itself. The only thing that doesn't make any sense it is that you are saying that it prints true for battery monitoring – Leo Dabus Jan 22 '18 at 15:34
  • 1
    https://www.dropbox.com/s/x91w8js9nleh615/BatteryLevel%20Sample.zip?dl=1 Tested your code and it works as expected. It prints "-1.0" only when running in the simulator and it prints false for battery monitoring also. – Leo Dabus Jan 22 '18 at 15:45
  • @LeoDabus , I am on real device not on simulator and battery monitoring is true. I know something i am missing or there is some another way to find out battery level. I have use this example https://stackoverflow.com/questions/27475506/check-battery-level-ios-swift – Sanjay Kumar Jan 23 '18 at 06:18
  • Have you tried the sample project I have posted above? If doesn't work try it using another device – Leo Dabus Jan 23 '18 at 06:20
  • could you give me another way to download sample project ... we can't access dropbox, google drive here for security reason – Sanjay Kumar Jan 23 '18 at 06:50

0 Answers0