Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

ios - How to monitor battery level and state changes using swift

So, I'm trying to figure out how to monitor the battery level and state changes in iOS devices.

So far I've determined how to get the current battery level, but can't figure out how to get the state or how to monitor any changes so I can pop a dialog (or notification, but I assume I can't monitor this in the background anyway so...) when 100% charged.

This is what I have so far:

@IBOutlet var BatteryLevelLabel: UILabel!
@IBOutlet var BatteryStateLabel: UILabel!

// function to return the devices battery level
    func batteryLevel()-> Float {
        return UIDevice.currentDevice().batteryLevel
    }

// function to return the devices battery state (Unknown, Unplugged, Charging, or Full)
    func batteryState()-> UIDeviceBatteryState {
        return UIDevice.currentDevice().batteryState
    }

override func viewDidLoad() {
        super.viewDidLoad()

         let currentBatteryLevel = batteryLevel()

        // enables the tracking of the devices battery level
        UIDevice.currentDevice().batteryMonitoringEnabled = true

        // shows the battery level on labels
        BatteryLevelLabel.text = "(batteryLevel() * 100)%)"
        BatteryStateLabel.text = "(batteryState())"
        print("Device Battery Level is: (batteryLevel()) and the state is (batteryState())")

// shows alert when battery is 100% (1.0)
        if currentBatteryLevel == 1.0{
            let chargedAlert = UIAlertController(title: "Battery Charged", message: "Your battery is 100% charged.", preferredStyle: UIAlertControllerStyle.Alert)

            chargedAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
                print("Handle Ok logic here")
            }))
            presentViewController(chargedAlert, animated: true, completion: nil)
        }

    }

Any assistance here would be greatly appreciated! Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the battery state notification UIDeviceBatteryStateDidChangeNotification and UIDeviceBatteryLevelDidChangeNotification to be notified when its state changed:

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "batteryStateDidChange:", name: UIDeviceBatteryStateDidChangeNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "batteryLevelDidChange:", name: UIDeviceBatteryLevelDidChangeNotification, object: nil)   

    // Stuff...
}

func batteryStateDidChange(notification: NSNotification){     
    // The stage did change: plugged, unplugged, full charge...
}

func batteryLevelDidChange(notification: NSNotification){     
   // The battery's level did change (98%, 99%, ...)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...