How to detect if the app was minimized IOS - ios

I'm fairly new to ios development. I'm actually coming from Flutter and have to make these native calls myself.
Basically, my app runs a timer/countdown (5-10mins). If the user minimises the app I want to stop the timer (e.g. user starts doing something else with the phone). If the user, however, simply locks their screen, I want the timer to continue.
I tried using some of the lifecycle calls like applicationDidEnterBackground and applicationWillTerminate, however, it seems both are called for minimization and screen off. How do I do it?

Related

Swift Xcode - how to differentiate between going to lock screen and going to home screen?

When the app enters the background or WillResignActive, both functions are called in my SceneDelegate. How am I able to distinguish between when the user exits the app to the HomeScreen versus just locking their phone?
EDIT: I want to perform a different function in each scenario

Is there a way to run a block of code of my application, when it is in the background? (SWIFT)

Let's say I would like to recreate the functionality of the default Clock app and its timer. When I set a timer, leave the application and proceed to do something else on my iPhone I get a UIAlertView displayed even though I am not currently in the Clock app, which means, that the code must have somehow ran in the background. How can I do that in my app?

ios background app is slow to respond from MPNowPlayingInfoCenter

I created a music app that is wired up to MPNowPlayingInfoCenter. I have a bunch of methods that do various things (next, pause, etc). However, when I hit the next button in MPNowPlayingInfoCenter, the app responds immediately and calls the method instantly when the app is active. However, when the app is playing the background and I hit next from MPNowPlayingInfoCenter, it responds but after a few seconds.
Has anyone else experienced this or have any idea what the culprit might be here?
You shouldn't use any graphics code from the background - iOS will kill your app if you do so too much as it takes away from the resources available to foreground apps. It'll also cripple your other background routines.

IOS: forcing update before app becomes visible

I have a timer in my app, and I want it to appear to be running while my app is on the background. I'd like that if the user presses the home button when the timer shows, for instant "01:11:11" and then goes back to the app and it becomes visible to him 10 minutes later, to see the timer as "01:01:11", however I get a split second where it shows the last state when the app went to the background ("01:11:11") before it starts updating from the correct time.
I assumed that I could correct this by updating the state of my timer in "applicationDidBecomeActive" and it did work on my simulator in Xcode but not on my Ipad.
I'm using cocos2d for my drawing and this is what I'm doing in my applicationDidBecomeActive:
CCScene *s=[director_ runningScene];
GameLayer *l=[(GameLayer*)[s getChildByTag:GAME_LAYER_TAG];
if (l!=nil) [l myUpdate];
I don't think it's relevant to the problem though because myUpdate does get called but I still have that split second glitch on my Ipad, as if it starts back from its last state no matter what.
In apples clock app, in applicationDidEnterBackground it hides the timer text, so that when the app comes into the foreground you see a blank UI for the split second where your app is loading the new timer data in the UI. Also, you may want to call some of your applicationDidBecomeActive code in applicationWillEnterForeground, which is called first. But keep in mind, applicationWillEnterForeground is not called when the app first launches.
There will always be a delay between when your app comes into the foreground, and when the UI updates. Theres no way to fix that, so you might as well use what apple uses to get around the issue.
Well I tried to hide my UI in both applicationWillResignActive and applicationDidEnterBackground. Since applicationWillResignActive is called first and before going into preview (double click on HOME) it causes a "not so pretty" preview but I thought at least it would solve my original problem. It didn't (not, on my IPad). It looks like the system takes the screen shot even before applicationWillResignActive.
I checked the timer in the official clock app and I see the clock is updating even when the app is in the background (in preview), so they "cheat" anyway...

Can I figure out in iOS whether the app is resuming from standby or from multitasking

I'm currently working on a change request for our iPad app that requires that I handle the resuming of the app in two different ways depending on whether the app returned from multitasking (the user was active in another app or on the homescreen and came back to the app) and standby (the iPad was switched to standby either through the standby button on top or by closing the Smart Cover)
In both cases the following methods are called in my AppDelegate:
applicationWillResignActive followed by applicationDidEnterBackground when I hit the homebutton to get tot he home screen or close the Smart Cover
applicationWillEnterForeground and applicationDidBecomeActive when I come back.
As the same methods are called I am a bit lost on how to detect where I come from on resume. All four methods have a single parameter passing in the UIApplication. I looked at its interface, but didn't find any useful clues.
Is there a way to differentiate between resuming from multitasking or standby?
There is no public api method to define why application did become active

Resources