Abhirajsinh Thakore
Abhirajsinh Thakore

Reputation: 1822

Is there any function which returns "time until Device Battery is fully charged"

I have found an answer to display battery percentage The Code is Following

class ViewController: UIViewController {

@IBOutlet var infoLabel: UILabel!

var batteryLevel: Float {
    return UIDevice.current.batteryLevel
}
var timer = Timer()

func scheduledTimerWithTimeInterval(){
    timer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(self.someFunction), userInfo: nil, repeats: true)
}
func someFunction() {

    self.infoLabel.text = String(format: "%.0f%%", batteryLevel * 100)
        }
override func viewDidLoad() {
    super.viewDidLoad()
    UIDevice.current.isBatteryMonitoringEnabled = true
    someFunction()
    scheduledTimerWithTimeInterval()
    // Do any additional setup after loading the view, typically from a nib.
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}}

The Problem is with the Remaining time

Thanks in Advance

Upvotes: 0

Views: 881

Answers (3)

Govaadiyo
Govaadiyo

Reputation: 6082

I haven't tried but get clue from this question/answer and convert to Swift.

You can use below NSNotificationCenter in your ViewWillAppear/ViewDidLoad.

NotificationCenter.default.addObserver(self, selector: #selector(self.batteryCharged), name: UIDeviceBatteryLevelDidChangeNotification, object: nil)

That will notify you every time cad will be called below code.

func batteryCharged() {
    let currBatteryLev: Float = UIDevice.currentDevice.batteryLevel
        // calculate speed of chargement
    let avgChgSpeed: Float = (prevBatteryLev - currBatteryLev) / startDate.timeIntervalSinceNow
        // get how much the battery needs to be charged yet
    let remBatteryLev: Float = 1.0 - currBatteryLev
        // divide the two to obtain the remaining charge time
    let remSeconds: TimeInterval = remBatteryLev / avgChgSpeed
    // convert/format `remSeconds' as appropriate
}

user this might be helpful in your case.

And Yes don't forget to remove NSNotificationCenter observe in "viewWillDisAppear"

Upvotes: 1

mag_zbc
mag_zbc

Reputation: 6992

You can try to measure the battery level over a period of time and calculate this yourself

var batteryLevel : Float = 0.0
var timer = Timer()

override func viewDidAppear(_ animated: Bool) {
    if UIDevice.current.isBatteryMonitoringEnabled && UIDevice.current.batteryState == .charging {
        batteryLevel = UIDevice.current.batteryLevel
        // measure again in 1 minute
        timer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(self.measureAgain), userInfo: nil, repeats: false)
    }
}

func measureAgain() {
    let batteryAfterInterval = UIDevice.current.batteryLevel

    // calculate the difference in battery levels over 1 minute
    let difference = batteryAfterInterval - self.batteryLevel

    let remainingPercentage = 100.0 - batteryAfterInterval

    let remainingTimeInMinutes = remainingPercentage / difference
}

Note that such estimate will not be very accurate.

Upvotes: 0

David Pasztor
David Pasztor

Reputation: 54755

UIDevice has no built in function for this. All you can do is to check the batteryState and when it is charging, measure the time between UIDeviceBatteryLevelDidChange notifications to calculate the charging speed and from that estimate the remaining charging time.

However, this approach will be quite inaccurate due to the fact that charging time is not directly proportional to battery percentage.

Upvotes: 2

Related Questions