IOS - Swift - callback when the phone connects

I have an Android app that uses BroadcastReceiver for older versions of Android and a JobScheduler-based implementation for Android 5.0+ that automatically launches my app when the phone connects to the charger (namely, it runs an IntentService to process some files).

I want to be able to do the same on iOS (in Swift). I found very few searches on Google and on this site, I think maybe NSNotificationCenter is related to this, but still I don’t know.

So, is there an equivalent to this in iOS?

Thank.

+4
source share
1

iOS , Android. , , .

, .

// Begins battery state monitoring //
UIDevice.currentDevice().batteryMonitoringEnabled = true
// Check for battery current status //
if (UIDevice.currentDevice().batteryState != .Unplugged) {
    print("Device is plugged in.")
}

, , . . , , App Store.

func applicationDidEnterBackground(application: UIApplication) {
    // Begins battery state monitoring //
    UIDevice.currentDevice().batteryMonitoringEnabled = true
    // Schedules NSTimer with intervals of 10 seconds //
    NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: #selector(batteryCheck), userInfo: nil, repeats: true)
}

func batteryCheck() {
    if (UIDevice.currentDevice().batteryState != .Unplugged) {
        print("Device is charging.")
    } else {
        print("Device is NOT charging.")
    }
}
+1

All Articles