1

Is it possible to somehow get a callback when the user presses on the physical power button on iOS? Just to make sure, I am not actually trying to change the behaviour of the power button, I just need to know when it was pressed.

Here is some extra info to explain why I need this callback. Our app uses a guided access mode to lock students in test and although guided access allows locking the device via the power button, that lock, unfortunately, causes our sockets to disconnect so we do not allow that. So what I need now is to detect the power button press so I can implement a fake lock in our app where I will dim the display and show a black image on top to save some battery power.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Saik
  • 993
  • 1
  • 16
  • 40
  • I'd be very surprised if you could stop the device from "locking" and you might consider alternative solutions based on your needs. Since you can't do socket comms while the app is in the background, you might consider using Remote Push Notifications to push data to the app. You might be able to detect a change in state of the App through the `UIApplicationDelegate` or because the socket connection on the remote end has been closed and push notifications back to the user stating that the service has been halted or limited. There are other possibilities, but need more context – MadProgrammer Dec 12 '18 at 23:34
  • @MadProgrammer so the phone does not even lock by default, you need to set a special option for it to be able to lock in guided access mode. We do have remote push notifications in place but we use the socket connection to monitor student phone state so we still need the socket connection at all times. – Saik Dec 13 '18 at 00:02
  • Creepy much :P ... – MadProgrammer Dec 13 '18 at 00:23
  • [Listen for iOS device power button presses](https://stackoverflow.com/questions/34013827/listen-for-ios-device-power-button-presses); [swift-home-vs-lock-button](https://github.com/ImEric/swift-home-vs-lock-button); [ios-home-vs-lock-button](https://github.com/binarydev/ios-home-vs-lock-button) ....? – MadProgrammer Dec 13 '18 at 00:25
  • I take those back, they won't work as those events are delivered guided access – MadProgrammer Dec 13 '18 at 00:35
  • Creepy? not really hehe. We are working on a calculator that can be used in tests and we need these issues to make sure that students are not exiting our app basically. We do not pull any information from the phone outside our application. – Saik Dec 13 '18 at 01:20
  • Is this App going to submitted to the App Store? If not, then you "could" take advantage of the "background audio" mode and simply play a silent audio file on infinite loop, which will prevent the App from been suspended. Just note, this is a clear violation of Apples usage guidelines and will not be approved by Apple. But, this won't tell you if the user has switched Apps, only that the App has been backgrounded. I'd be interested in knowing your use-case of how the student gets out of guided access mode - assuming that they find the pin code, then the above suggestions would also work – MadProgrammer Dec 15 '18 at 23:07
  • They can freely exit the pin mode by entering the password that they themselves assigned when setting up the guided access mode. The app detects the change of guided access mode and sends a socket event notifying the server that student left pin mode and this results in teacher getting notified basically. The app is distributed through app store so yeah :) that background sound thing is a no go. – Saik Dec 16 '18 at 05:47
  • So, when the goes into the background, you a grace period (roughly 30 seconds) before the app is suspended. My “guess” is, if you know when guided access is disabled AND the app has entered the background, you have a short opportunity to send a notification to the server. Just an idea – MadProgrammer Dec 16 '18 at 06:31

0 Answers0