swift - Is there any function which returns "time until Device Battery is fully charged" -
i have found answer display battery percentage code 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() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. }}
the problem remaining time
thanks in advance
you can try measure battery level on period of time , calculate
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 difference in battery levels on 1 minute let difference = batteryafterinterval - self.batterylevel let remainingpercentage = 100.0 - batteryafterinterval let remainingtimeinminutes = remainingpercentage / difference }
note such estimate not accurate.
Comments
Post a Comment