Check if battery charging status changes in real time Swift

I want to check if my iphone is charging or not and what percentage. Im able to get the percentage fine as well as knowing if its plugged in or not when i start the app, however im not able ot make it change status as i unplug the charger. Essentially i want to be able plug and unplug the phone and have my Charge.text field change status from "charging" to "unplugged" and vice versa.

i implemented an alert box but that also only pops up when i start the app. ive got the core location in there because ive got an idea on implementing a location feedback thing, but its not used now, please just ignore that.

when searching the forums i seemingly only get answers on on how to check it once, not continuous updates. i dont know if the "real time" description helps but i didnt really know how to describe it. please help :)

import UIKit
import CoreLocation
import Alamofire
import SwiftyJSON

class ViewController: UIViewController, CLLocationManagerDelegate {
    @IBOutlet weak var Charge: UILabel!
    @IBOutlet weak var resultsField: UILabel!
    var chargingVar = ""
    var batteryLevel: Float {
       return UIDevice.current.batteryLevel

    func batteryStatus() {
        UIDevice.current.isBatteryMonitoringEnabled = true
        switch UIDevice.current.batteryState {
        case .unknown:
          self.Charge.text = "BATTERY: UNKNOWN!"
        case .charging:
          self.Charge.text = "Charging..."
        case .full:
          self.Charge.text = "BATTERY full"
        case .unplugged:
          self.Charge.text = "unplugged"
            self.Charge.text = "nope"
    func batteryStateChanged(){
        if (UIDevice.current.batteryState == .charging) {                UIApplication.shared.isIdleTimerDisabled = true
            self.chargingVar = "is charging. \u{1F603}"
            self.chargingVar = "is not charging. \u{1F622} "

    func chargingAlaer(){
        let alertController = UIAlertController(title: "Charging Status",
                                                message: "Your device \(chargingVar)",
            preferredStyle: UIAlertController.Style.alert)
        let ok = UIAlertAction(title: "OK",
                               style: UIAlertAction.Style.default,
                               handler: {(action) -> Void in
        self.present(alertController, animated: true, completion: nil)

    @objc func displayBatteryCharge() {
       self.resultsField.text = "\(batteryLevel * 100)%"
   override func viewDidLoad() {
       UIDevice.current.isBatteryMonitoringEnabled = true


   override func didReceiveMemoryWarning() {
    override func viewDidAppear(_ animated: Bool) {




This question is more about Apple's framework, or is tightly related to iOS development. I'd suggest that you ask questions like this over at or instead.

ok, im sorry, i thought this forum was also for development related questions? ive tried implementing the StateDidChange thing, but i didnt get it to work, thats why im asking here. people have helped me with code in this forum previously

Only if it relates to using the language. A good measure for applicability to these forums is to ask yourself a couple of questions:

1 - Would this question be applicable if I wasn't writing the code in Swift? Am I asking about frameworks that are specific to a platform, like the Apple frameworks, that would be applicable even if I was writing the code in C, Objective-C, or some other language?

2 - Am I asking a question about a specific device(s),or operating system? Your battery status issue is really about iOS and iPhones/iPads, not about Swift.

If the answer is "Yes" to either, then it's not really a subject for these forums. People may or may not help out, and no one is going to get thrown off for helping if they can, but, you'll probably have better luck getting an answer elsewhere because there will be a bigger audience of folks with the technical knowledge to give you help.

ok, sorry for offending. im new to coding so i got no idea what c or objectiv-c is or how it differs from swift. im sorry if that makes me to novice to join the forum - i didnt know. there is nothing wrong with my iphone, im trying to make an app to monitor these things - so no issues with my battery. ive got an idea for an app and am trying to realize it is all. im trying my hands on any and all help i get is all.

No offence taken. You might want to look up C and Objective-C on the internet to get an overview for historical purposes and to understand the differences between Swift the language, and using Swift to program for Apple systems.

I' m not saying there is anything wrong with your battery. My point is that you are looking for a notifcation from the system (iOS) when the state of your battery changes (charging versus not charging, plugged in versus not plugged in). This architecture is specific to the iPhone, iOS, and the Apple philosophy, not Swift itself, which is just a programming language, like C and Objective-C. Those types of questions are better asked on Apple-specific forums (Apple developer forums, StackOverflow, etc.) because there is a higher probability that someone that is following those forums will have either insider knowledge about how iOS and the iPhone work, or will have encountered your particular issue, and figured out a solution.