The_Enigma
The_Enigma

Reputation: 117

Detect when iPhone is being unplugged and plugged in

I am just trying to write simple code that detects when the iPhone is unplugged and then plugged in. But being event-driven, instead of using a while loop. This code does work, but doesn't detect if the phone is unplugged and will not update the printed text.

EDIT In the long run I just want the phone to play a sound whenever the iPhone is unplugged. (Sorry for leaving the end goal out.). I was using this print statements just so I can make sure it was working.

func startCharger()
    {

        printscreen.text="Started charger protocol"

       UIDevice.current.isBatteryMonitoringEnabled=true

        if(UIDevice.current.batteryState == .unplugged)
        {
            printscreen.text="Battery is unplugged"
        }
        else
        {
            printscreen.text="Battery is plugged in"
        }
    }

Upvotes: 2

Views: 1732

Answers (2)

dscrown
dscrown

Reputation: 578

Quick implementation of Dule' code. Check if the power is connected. Copy these functions into your class. Call isPowerConnected() in the viewDidLoad

func isPowerConnected(){
    UIDevice.current.isBatteryMonitoringEnabled = true
    NotificationCenter.default.addObserver(
        self,
        selector: #selector(batteryStateChanged),
        name: .UIDeviceBatteryStateDidChange,
        object: nil)
}

var chargingVar = "" //Empty Variable

func batteryStateChanged(){
    if (UIDevice.current.batteryState == .charging) {    //Here we check if the device is charging
        UIApplication.shared.isIdleTimerDisabled = true  //Here we disable the lock screen time out value
        self.chargingVar = "is charging. \u{1F603}" //Here we change the variable to "is charging" 😀
             chargingAlaer()    //If power is pluged in we send an Alert
      }else{
        self.chargingVar = "is not charging. \u{1F622} "  //Here we change the variable to "is not charging"  😥
             chargingAlaer()         //If power is not pluged we send an Alert
    }


}

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

Upvotes: 1

Dule
Dule

Reputation: 499

You can get a notification on battery state change with UIDeviceBatteryStateDidChange, the code looking something like this:

NotificationCenter.default.addObserver(
    self,
    selector: #selector(batteryStateChanged),
    name: .UIDeviceBatteryStateDidChange,
    object: nil)

Of course, you need a method in ViewController (or wherever you want to get notification). In this example:

func batteryStateChanged(){
    // Your logic
}

Upvotes: 1

Related Questions