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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -