Phone unlock/lock detection

Hi,

I'll explain my question through how whatsapp does it.

When the phone is locked then whatsapp routes call through apple's native callkit

When unlocked, pressing accept essentially redirects to whatsapp and then whatsapp handles the call from there.

However, this component of unlock detection is what I'm not able to find any info about.

Essentially,

how i do it is:

let isPhoneLocked = !UIApplication.shared.isProtectedDataAvailable isProtectedDataAvailable == true → device is unlocked isProtectedDataAvailable == false → device is locked

The problem is that if the phone has been recently unlocked, then protected data is still available on the phone even after the lock for the next 10-40 seconds. So theres a false positive.

I want there to be a foolproof and robust way to do this. And I'm not entirely sure how

Answered by DTS Engineer in 867163022
It's possible that the keychain APIs might track lock state more closely

They do not.

Using the data protection state as a proxy for the device lock state is a mistake. There are numerous ways in which this can fail, with the biggest one being that it’s a complete non-starter if the user doesn’t have a passcode set on their phone.

There’s no supported way to track the device lock state specifically. If you’re building a VoIP app then, as Kevin noted, CallKit is the recommended way to wrangle you’re UI. If you’re not building a VoIP app, please clarify your goals so that we can see if there are any good options. What are you actually planning to do with this information?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

First off, a clarification here:

When the phone is locked, then WhatsApp routes calls through Apple’s native CallKit.

When unlocked, pressing accept essentially redirects to WhatsApp, and then WhatsApp handles the call from there.

Are you sure this isn't the standard CallKit flow? Have you tested this with Speakerbox? It's possible I'm misunderstanding, but what you're describing sounds like how CallKit works by default. More specifically, accepting a call on an unlocked device opens the target app (as well as starting the call), and most apps present their own call UI when this open happens.

Keep in mind that you can't actually implement a good call experience without using CallKit. For example, without CallKit, any other incoming call from any other app will immediately interrupt your call, effectively hanging up your current call. Note that this is not a theoretical problem but is in fact the primary developer complaint that led us to create CallKit.

The problem is that if the phone has been recently unlocked, then protected data is still available on the phone even after the lock for the next 10-40 seconds.

Yes. This is basically just how data protection "works".

I want there to be a foolproof and robust way to do this. And I'm not entirely sure how.

I'm not sure there is one. It's possible that the keychain APIs might track lock state more closely, but, to be honest, I don't think so.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

It's possible that the keychain APIs might track lock state more closely

They do not.

Using the data protection state as a proxy for the device lock state is a mistake. There are numerous ways in which this can fail, with the biggest one being that it’s a complete non-starter if the user doesn’t have a passcode set on their phone.

There’s no supported way to track the device lock state specifically. If you’re building a VoIP app then, as Kevin noted, CallKit is the recommended way to wrangle you’re UI. If you’re not building a VoIP app, please clarify your goals so that we can see if there are any good options. What are you actually planning to do with this information?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi,

I mostly used an LLM to write the code so far so I was never in the weeds enough to try looking at the documentation and went by my logic. If callkit natively supports such functionality then I'll be taking a closer look.

Thank you for the help. I will update this once I have tried it out and let you know

Phone unlock/lock detection
 
 
Q