Alternative for running a function in background mode - swift

I am creating an app where it will send you a local notification every 20 minutes. The problem I am having is once the app goes into background mode, the timer which triggers the function to run which executes some code then sends the user a notification won't run. Is there any alternative way of doing this? This timer runs on a loop, reseting its self every 20 minutes and running a function.
Thanks

Sure. Check out the documentation for UILocalNotification. You can set a fireDate to the exact second when you want the notification to go off. It also has parameters you can use to specify a repeat interval.
You cannot force the user to activate the app to run your function. Once the user taps the notification and your app becomes active, you can then perform all the logic you require (perhaps also including dealing with past "missed" notifications) .

Related

How can I keep a Timer running in the background?

I have a timer that continuously executes a function, but it stops whenever the user exits the app, changes to a different view, or closes the app. How can I keep the timer active and running in each of those scenarios?
Maybe save the time on database and pull the time whenever user open the apps again.

Can we stop the for loop when app goes background

The for loop is running fetching the data(images and text) from array but when i press home screen button the loop still continues in background and lasts more than 5 seconds which is default time, it should not be more that that as i studied while googling. And when i press home button even then the app delegates respective method like appEnterBackground also called after completion of this loop containing method. So, is it possible to break the loop when the home button is pressed.
Please guide.
If you wanted to detect the entering into background, in addition to responding to the app delegate applicationDidEnterBackground method, you could alternatively register yourself as an observer of the UIApplicationDidEnterBackgroundNotification notification (e.g., using the addObserverForName method of [NSNotificationCenter defaultCenter]).
Note, if you want to cancel your requests, you will want to ensure that (a) the requests are run asynchronously; and (b) they're cancelable. If you're targeting iOS 7 and later, you can accomplish this with NSURLSession (e.g., dataTaskWithRequest or dataTaskWithURL which return a NSURLSessionDataTask object, on which you can call the cancel method if and when desired). Then you can write a handler for the notification that cancels any pending requests.
If you really want to cancel the requests, you can do something like the above, but you alternatively could simply request additional time to complete the requests if the app happens to go into background while the requests are running. This way you get a few minutes to finish requests rather than just a few seconds. See Executing Finite-Length Tasks in App Programming Guide for iOS: Background States.
Or, perhaps even better, you could add your tasks to a background NSURLSession. See the Downloading Content in the Background section of the aforementioned App Programming Guide for iOS: Background States. This way the tasks will continue even after your app is suspended (or, if you app is terminated due to memory pressure). For more information, see WWDC 2013 video, What's New in Foundation Networking.

How to build an alarm clock/reminder function in LWUIT?

I'd like to implement an alarm clock/reminder function in a Lwuit application, where the user may set an alarm time, and at that time a reminder dialog pops up with an alarm sound. However, I'm unsure about the approach. So help on any of the below questions would be greatly appreciated:
How to keep a timer running constantly, no matter what Form is displayed, e.g. without interfering with lwuit and EDT?
What's the right way to implement the trigger mechanism that will make the reminder Dialog box pop up? This may happen at any time, no matter what Form is currently displayed and shouldn't interfere with the ongoing user operation, so that when the reminder dialog is closed the user can continue whereever he was when the reminder popped up.
And finally, when the application is closed, or not running in the foreground, how can I keep a timer running? With the new and exiting CodenameOne coming up, I'd obviously like the solution to work on both J2ME, iOS and Android.
Sorry, if the answers are obvious, but I haven't been able to find a solid working solution.
1) You can use java.util.Timer for a generic timer, its callbacks will occur off the EDT so in order to use LWUIT you would just need to callback into the EDT using Display.getInstance().callSerially() and have your code within the Runnable callback.
2) In order to not block input and display in an unobtrusive way you can use the glasspane to just show a translucent element on top of the screen. It will not block input in any way. There is a sample of that in the LWUIT Chat demo application.
3) You can't. Background process execution isn't really supported on any device, all devices have a special "use case based" solution for background execution which requires you to write your applications in a very specific way. Codename One will add support for push notification which will allow you to push an alarm from your server. Seams counter intuitive at first but if you consider the amount of battery life that will be taken up by background processes it becomes pretty obvious why this was done.

Can I cancel the method "applicationDidEnterBackground?

I wanted to cancel the method, to request a confirmation from the user when he presses the button "HOME" where on iPhone would go into the execution in background.
If the user accepts, enters into the execution in background, if not accept, I do not do anything.
I looked in the FORUM, in the documentation from Apple and I found nothing.
Could anyone help me?
Thanks in advance!
No you can't override this. The event will always fire but it does give you time to clean up before you move to the background.
Your delegate’s applicationDidEnterBackground: method has approximately five seconds to finish
any tasks and return. In practice, this method should return as quickly as possible. If the method does not
return before time runs out, your application is terminated and purged from memory. If you still need more
time to perform tasks, call the beginBackgroundTaskWithExpirationHandler: method to request
background execution time and then start any long-running tasks in a secondary thread. Regardless of
whether you start any background tasks, the applicationDidEnterBackground: method must still exit
within five seconds
iOS Application Programming Guide
No, you can't override the operating system or the user's button press.
If the user presses the button to send the app into the background, the app goes into the background.

UILocalNotification handling when screen is locked

I am developing an iPhone app that delivers alerts at certain times using UILocalNotifications. Pressing the OK button on the alert launches the app so it can perform specific tasks.
So far, everything works beautifully in most cases: if the app is running and the alert fires, it works fine, and if the app is not running (but the phone is on), it works just as well.
However, if the alert fires when the screen is locked, one of two things happens.
alert fires, and I "slide to unlock" immediately, then the app launches as expected.
alert fires, but I wait to "slide to unlock" longer than 20 seconds (the time it takes the screen to lock (dim) again). When I finally unlock the screen, the alert shows, but does NOT launch the app.
I have looked throughout the Apple docs and this site, but cannot find an answer. I hope my explanation makes sense. Any thoughts?
I think that behaviour makes sense and shouldn't be circumvented (which I think is not possible). If the user slides to unlock just after a few seconds after the notification, the propability is high, that he slided to unlock just because of that notification and wants to get into the app. If a lot of time is passed, the notification is still shown, but the propability is low, that the user slides to unlock because of this notification. It is more likely that he just wants to write an email or do something completely different. So the app of the last notification shouldn't get started.
Thanks for your quick and clear answer, Dominik. Quick follow-up: the purpose in relaunching the app is to schedule another alert. If I send several alerts at once (scheduled for different times), and the user does not unlock the phone for any of them, do they all appear at the same time the first time the user unlocks? (I would only want the last one to appear)
Thanks again.