I used NSTimer to call a method after 1 sec when app goes in background and after nearly 17 minutes timer stops working.
When it came back in the foregound it again started working, so please tell me why this is happening and how to resolve the issue.
I also tried using perform selector with delay in recursion for the same purpose, but again giving the same result. Please suggest any solution. Any help will be completely appreciated.
Wierd issue- i too had this one, but the following worked for me.
//Run the timer on the runloop to ensure that it works when app is in background
[[NSRunLoop currentRunLoop]addTimer:self.timer forMode: NSDefaultRunLoopMode];
From Implementing Long-Running Background Tasks:
For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:
Apps that play audible content to the user while in the background, such as a music player app
Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Newsstand apps that need to download and process new content
Apps that receive regular updates from external accessories
If few minutes isn't enough, you can't make an app run infinite time in backgorund unless it uses:
Audio Playback
Location Services
External Accessory
You can use Background Tasks to get 10 minutes of running time for all other apps, or Local Notifications to notify user.
Related
Is it possible to put your app in the background, and have a counter that once expires, wakes up the app and have the app does some action? I know it's basic, but I just cant seem to have it work. Where do I put this counter + action? Under app did enter background?
Thanks for the help.
I don't think that this works in general:
You are allowed only to run in the background for specific tasks:
Apple doc tells which tasks that are:
One of that tasks is receiving GPS messages.
As long as you have GPS enabled and your app configured that it uses GPS for background, your app stays alive in the background.
If you disable GPS some time later it will suspend, and not wake up till the user activates it.
So to realize your problem you have to stay active in the background (e.g by reading GPS).
You can start the timer in AppDelegate:applicationDidEnterBackground or similar
If you need more time to shutdown, you explicitly can request for more time, there is one method for that. I dont know what happens if you request more at regulary intervals
I'am developing an application that keep a WebService informed for device location each 5minute (for exemple) :
So when the application leave the foreground execution and enter background I have to lunch a timer who
1 - Update the device location
2 - Send the location to a web service
How can I perform this action ? Or do you know any code exemple that I can follow to achieve this design ?
Thank you for your help !
Doesn't look like anyone gave you a solid answer, so allow me.
There is no iOS approved way of running a 5 minute (or any minute) timer in the background (unless your app is VOIP or music). What you CAN do is register your app as requiring location services in the background (edit the info.plist and add a key Required Background Modes and then add a value App registers for location updates. What this means is your locationManager:didUpdateToLocation:fromLocation: method and corresponding region monitoring/SLC methods will fire on location change, but Timers will not work at all.
The reason why is that timers require a run loop (a thread executing code) to be running and must piggy-back on that thread, but when the app is in the background even when executing code from the LocationManager, the run loop that executes the code almost always finishes before your timer would go off.
Hope this helps!
you cannot do this (on iOS).
(unless your app requests permissions that are meant for Navigation apps)
Hello #Aladdin Gallas,
I have developed a simple application that supports background location updates for iOS and Android in Xamarin.
The app pushes a new location every 2 seconds (you can change it to 5 minutes if you want to).
There is a feature I haven't been able to add; it is to keep the service up when the user closes the app (for iPhone). Other than that, as long as the app is open, even if the phone is locked, the service keeps running.
You can take a look at the application as a reference if it helps.
GitHub Repo
I have an iphone app that has a 30second process that does some network IO. Basically, while the app is in the background, i want this process to run every hour (actually once a day, but if it fails i want it to re-run in an hours time).
With the background features of ios 4, is this possible? If so, how? What are the limitations that i'll come up against?
Thanks so much!
Take a look at Apple's documentation about running code in the background.
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html
There are few different ways of approaching backgrounded tasks. The only apps that can have fully backgrounded processes are "audio", "voip" and "location" apps, and this needs to be declared in the Info.plist.
If your app is not of this type, you'll probably find it difficult to do what you want easily. There are methods which allow you to keep your app alive in the background for a finite period of time (also at that link), but eventually your app will be shut down.
Local Notifications will only prompt the user to open the app - do you really want to have an alert pop-up on the phone every 30 seconds?
I was making some kind of similar research, have a look at this SO answer in case you didn't manage to find it before. Applications like DataMan or Data Usage must have some sort of periodic code execution in the background, so I'm not 100% convinced that what you're asking for is impossible..
I believe that Using Local notifications will help....
check following....
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1
An application can create and schedule a local notification, and the operating system then delivers it at the schedule date and time. If it delivers it when the application is not active in the foreground, it displays an alert, badges the application icon, or plays a sound—whatever is specified in the UILocalNotification object. If the application is running in the foreground, there is no alert, badging, or sound; instead, the application:didReceiveLocalNotification: method is called if the delegate implements it.
The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.
I have noticed that some apps e.g. Skype run in the background.
I would like to have my app run in the background also, waking up every 1 seconds to update some data and then going to sleep again.
How can I do this?
I gather that NSTimer's do not work in the background.
You can't. Voice-over-IP apps get a special exception for this, basically the system manages a network socket for them and wakes them up if there's data. There's no way to do the same with a timer.
By the way, waking your app every second, your battery wouldn't last half a day.
See Executing Code in the Background
You would need to use push notifications to push information to the app (like the Facebook or Skype app). See this documentation from Apple on how to use push notifications.
Not every second though, that would drain the battery. Other than that, there isn't really a way unless you are running a VoIP app.
In my iPhone app I would like to run several queries when the
application is in background.
I already use ASIHttpRequest to make the queries, that works fine but
now I try to find a way to trigger them in background.
In the app delegate, I have added a call to the method making the request:
[self getItemsFromServer]
getItemsFromServer runs an asynchronous request (on the simulator I
saw the log of this methods once I get back the application to the
foreground).
How can I use some kind of timer to have this method ran every 10
minutes (I just need to run it 4 or 5 times, not each 10 minutes until
it goes back to foreground :-) )?
thanks a lot,
Best Regards,
Luc
iOS4 allows your app to run for X amount of time, granted that iOS4 grants you the time you request. Check out: Completing a Long-Running Task in the Background.
Specifically,
Any time before it is suspended, an application can call the beginBackgroundTaskWithExpirationHandler: method to ask the system for extra time to complete some long-running task in the background. If the request is granted, and if the application goes into the background while the task is in progress, the system lets the application run for an additional amount of time instead of suspending it
You could probably use Task Finishing to do that. In iOS you can mark a thread as finishing and give it a specific time to live. This would let you do a few more calls to your web server.
Have a look at Executing Code in the Background
Actually, you are specially not allowed to make general HTTP calls while in background. The only apps that can be active in the background are those that play audio, do location or are running VOIP calls. I believe Apple's whole philosophy with background is that apps shouldn't be doing 'work' other than these limited cases because there are limited resources available. THe suggested way to work around this is to use (ugh) notifications or just do a refresh when your application wakes up. The doc that willcodejavaforfood references explains this.