Reputation: 1
I am looking to keep track of the amount of time users have spent in my application. I want to stop tracking this once the user interacts with any other application, but I do not want it to stop tracking this if the user locks their phone and re enters the application without interacting with anything else. is this possible in ios?
Upvotes: 0
Views: 273
Reputation: 299565
"Interacts with any other application" is not something you can determine, and that's intentional. Even if you found some tricky way to achieve it, you should expect Apple to break it in the future because it's a privacy concern.
Even more critically, "any other application" is one of those things that sounds very precise but is actually incredibly vague. Is interacting with a share sheet (which may be an extension to another app and occurs outside your process) "interacting with any other application?" How about a custom keyboard provided as an app extension? Is iPad multitasking "interacting with any other application?" How about receiving a phone call (i.e. Phone.app)? What if the phone call is answered, but the user comes right back to your app? If your app is running when the device powers off (intentionally or due to power loss), is that the same as "locks their phone?" There are lots and lots of corner cases.
That said, you can detect a lot of the pieces you're describing via the application delegate (or the related notifications). You can tell when you resign active and go into the background, you can tell when the device is locked (via protectedDataWillBecomeUnavailableNotification
). With that, and some heuristics around timing, you can determine a lot. For instance, if you resign active and then protected data immediately (or ~10s on older OSes) becomes unavailable, you know you were likely active when the user locked the device. Even more accurately, you can see that protected data become available immediately before your app becomes active.
Almost certainly the want to deal with this by watching notifications (or app delegate methods) and the clock. If you resign active and then become active in a very short period of time, then there probably wasn't another app in there.
One side note: protectedDataWillBecomeUnavailableNotification
is generally only posted if device data protection is enabled, which requires the user to have a PIN. If the user has no PIN, then it may be a bit more difficult.
Upvotes: 1
Reputation: 535890
Probably not, because your app is backgrounded and suspended in both situations and you have no means of distinguishing them.
Upvotes: 1