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

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?

Related

How to detect if the app was minimized 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?

Application functionality changes when run on device

I've found that my application stops its timer when the device goes to the lock screen on actual device, but it works perfectly on the simulator.
Is that because I haven't released the app, so an actual device won't let me keep working on the lock screen? Or is it a bug in my code?
I've written an NSLog() statement to monitor my application's timer action, it keeps working when the simulator goes to the lock screen,
but on an actual device it stops.
I'm so frustrated because I don't know why there are these differences between the device and the simulator, and I don't know how to solve it.
What you need to do is implement applicationWillEnterForeground and applicationDidEnterBackground to persist the state of your timer, and resume it whenever the app opens again.
You may also want to use UILocalNotification to schedule a notification when your timer finishes.
Here is another post about using UILocalNotification with timers that you may find useful.

IOS App and Background

I have a music signal processor app I want to publish and it needs to run in the back ground. I used to Info-plist to do so with UIBackgroundModes. It runs on IOS7 or less, and the problem I am having is once the phone locks and the app goes into the background I can't get it to come out of the background and its keeps running with the red banner at the top. Some of this I have learned as I go, but I have noticed some run on a timer, but I want mine, since its a music processor to keep going until the phone either unlocks or whatever interrupted it stops.
The app is basically a stomp box used with iRig. If that helps.
Thanks
Can you clarify about what you mean when the phone locks, the app goes into the background and yo can't get it to come out of the background? Are you saying like the app is playing music while the app is locked?

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.

Monitoring Significant Location Changes, App closed GPS signal (Arrow)

First of all I hope don't repeat any topic, I have spent a lot of time reading on internet about this before ask.
I'm developing an app for iOS => 5 and I need get the user location, but I don't need always a great accurate position, so when the app goes to background I change the normal location mode to Significant Location Changes and when the app come to foreground I stop the significant location change mode and get back the normal location mode, that's all goes fine,
my problem is when the app goes to background mode or even the user KILL the app the GPS signal (the purple arrow) still appear ALWAYS, even after restart the device, the only way to make disappear it is un-installing the or disabling the significant location changes when the app goes to background, but I need this mode working when the user goes to background mode, maybe not when the app is killed.
I don't know how to stop the service when the user kill the app... because event "applicationWillTerminate" is not called if you have multitask ON.
My main reason because I don't want the purple arrow appear always, even the app is killed is because I don't want the user think the app is drying the battery... so the user won't want the app installed.
First question: Is normal that the purple arrow appear always, even the app is killed if the Significant location change is enabled for the app?
Second question: Is any way to change this, to stop the service at least when the app is killed to hide the purple arrow?
Thank you all.
Sorry for my english if something is wrong.
After few days researching to be sure about this service, seems like there is NO way to use significant locations change in the background without the arrow in the top bar. I have been testing with different apps and reading through internet and nothing to solve this.
The only ""way to solve"" this is disable multitask mode in the app and disable the significant locations change in the event "applicationWillTerminate" but don't make sense... I'm losing the background location always not only when the app is killed... so is not useful.
It's bad... is a really good service to get locations saving battery but as user and developer I don't want an app that make appear this icon ALWAYS even after the app is killed, as developer I don't want that people uninstall the app for this reason and as normal user I don't know what mean this icon if is normal GPS or Significant locations change and what mean this... if my battery is being drain or not.
The other option to get locations in background task but has a big impact over the battery....
I hope apple fix this in future versions... adding at least a new arrow or showing only when a new location arrive instad of always or allowing to stooped when the app is killed.
All this has been researched over iOS - 6.1
Thanks to all.
When you use the significant location change service, the OS keeps your app running. If the app is in the background and is killed, the OS restarts it, so the purple arrow will always show. When a new location is detected, the app is restarted. From the docs:
If you leave this service running and your app is subsequently suspended or terminated, the service automatically wakes up your app when new location data arrives. At wake-up time, your app is put into the background and given a small amount of time to process the location data. ... To stop the significant change location service, call the stopMonitoringSignificantLocationChanges method.
Hmmm, interesting. I've just seen that problem with another app - seeing the arrow still active made me want to disable location updates for that app so the problem is real. Best thing I can think of is to have some kind of timer to disable the location monitoring after a certain amount of time if that is appropriate for your app.

Resources