De/activation of an app based on charging state?

Ladies and gentlemen,


one of my clients would like an iOS application, which would allow him to control his HomeKit devices in a special way, and which would keep active when the device is in its charging dock, continuously informing him of the current state.


I can do most of the things desired, but there are two requirements which I can't find any way how to do:

  1. launch/activate my application automatically whenever the device is put to the dock (=starts charging)
  2. when in charger, launch/activate my application automatically whenever the device would normally sleep the screen.


In other words, the client would want the special HK controller activates whenever the device is put to the charger, and also — only when in charger — whenever he stops using the device in the normal way for the idle interval (whilst, obviously, if not in charger, he wants the device to normally switch off the screen and lock).


Is there any trick to do either of these two things or ideally both? Whatever I try, I can't find any. Thanks for any idea!

Replies

I understand you want to keep your app active when charging.

Isn't it what iOS does when it updates iOS while charging ?


Could this discussion help you ?

https://discussions.apple.com/thread/8180364

Well thanks, but it's a bit more convoluted than that.


He obviously does not want to switch the auto-lock completely off; that would clash seriously with the normal usage, when the iPhone is not charging (or he would have to remember to switch is always back on whenever he takes the iPhone from the dock, which is self-evidently absurd).


The good news is that this is actually not needed at all! Once my application is active, it can both monitor the charging status (through the batteryState property) and, if the iPhone is being charged, it can easily keep the screen unlocked through idleTimerDisabled.


The bad news is that to be able to do this, the app needs to be active. That's why I need a way to automatically launch/activate the application when the device is put into the charger — that's one thing whole solution I can't see :/


(The other one is to make the app automatically launched/activated if the charging is in process, another app is active, and the idle timer strikes in: normally iPhone would switch off the display in this case, I need it again to activate my app.)


Unless I am missing something of importance, that's all I need; all the other requirements I do know how to implement easily.

So, if I understand, you want to use some notification of charge to launch/activate your app ?


Do you want to launch the app or only to activate from background ?

As far as I understand, the first is not possible:

https://stackoverflow.com/questions/32179508/how-to-launch-app-automatically-on-recieve-local-notification-without-click-on-n


For the second, you could detect that the iPhone has been plugged / unplugged

https://stackoverflow.com/questions/41129725/detect-when-iphone-is-being-unplugged-and-plugged-in


Hope that can be of some help.

> you want to use some notification of charge to launch/activate your app?


Essentially, yes. More precisely, I need the app to be launched/activated somehow when charging begins; I do not really care whether the mechanism used for that is a notification or anything else.


> Do you want to launch the app or only to activate from background?


Well as “launch/activate” sort of suggest, I would need it to be activated if it happens to be suspended (or runs, which it very probably would not, see below) at bacground; launched if it does not run at all.


> As far as I understand, the first is not possible


Actually, a local notification (alerting the user who would tap the alert to launch the app) would be quite an acceptable solution — provided that notification could come exactly at (or very shortly after, let's say up to a second or two at very worst) the moment when the device starts charging.


Is that possible? As always, I can be missing something of importance, but I don't know such a local notification can be requested. Is there a way to do that? All I know are time- and location-based local notifications, none of which helps here the slightest bit.


> For the second, you could detect that the iPhone has been plugged / unplugged


Absolutely, provided my application actually runs at background, which is generally — again, far as I understand — not possible to achieve.


Again do please correct me if I am wrong, but far as I know, Apple does not allow applications to run at background, unless they play a (silent, in this case) audio, which is trick dirty enough for my client to frown upon, not speaking of the Store, which well might reject such a thing. There's also BGTaskScheduler now, but it won't make sure my app gets a few ticks of CPU at least each second, which is roughly what I would need; the new API, far as I understand the thing, might well leave the app dormant for minutes (or hours or days) 😟


If there's any other possibility to make my app really run at bacground (well, aside of VoIP which is not a solution for roughly same reasons as the audio-play hack) — or, even better, be reliably resumed at background each second or so —, I never heard of the thing, and would be grateful if you point it to me.


Thanks and all the best,

OC

AFAIK, You can receive notification when in background, but nore sure timing is guaranteed.


I guess you've read this doc already:

https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SchedulingandHandlingLocalNotifications.html


This may help:

https://stackoverflow.com/questions/45259797/ios-10-receive-remote-notification-in-background