I am working on the iOS app, that needs periodic downloads from the server both in active and background mode. I know that for the Background mode there is a possibility to use Background Fetch functionality. However, I did not find if Background Fetch works in active mode as well.
Can some body tell me if it does work?
Or if it doesn't, what is the best solution to get some data periodically on iOS app (basically indefinitely until the app is terminated)?
The way to achieve what you want to do is by using background fetches. (as you are doing).
As the apple documentation saids:
The system wakes the app at opportunistic moments to begin downloading
new content.
That means that your app doesn't needs to be in background mode, it will be waked up.
This method will run over arbitrary intervals, depending on how well your app behaves in terms time consumption and energy usage.
Related
Can an app go into background execution and minimize itself, but execute a code to perform a certain action (recording the screen to be specific) in iOS Swift.
Programming Guide: Background Execution
This should help get you started. But you can't do what you want to do for longer than three minutes and you won't be able to put in on the app store.
My app works like this - you press an uibutton and it starts appending data inside a tableView with a specific delay, cell by cell. Sometimes it may take hours to append, because of the manually set delay parameter.
The problem is each time i switch it to background - the app gets suspended after several minutes (up to five usually) and the whole process breaks.
Is there any approach to handling this situation?
Thank you.
That's the designed behavior, an app return to background normally only have very short active time, then it will be suspended, but there are some exceptions:
Apps that play audible content to the user while in the background, such as
Music player app
Apps that record audio content while in the background
Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Apps that need to download and process new content regularly
Apps that receive regular updates from external accessories
Basically speaking, if your app need to interact with outside for resource that can not be generated inside your app, you can apply for background running.
Any every app can legally apply a background task to run for a short time when the app enter background mode.
In your case, even if you implemented the background running, your app will possibly be rejected. If your app is doing the adding to table action controlled by a timer, you should be able to simulate the behavior yourself.
save a the system time stamp locally, maybe in UserDefault
when app launch or enter foreground, get the system time, you can calculate the time difference and figure out how many actions you should take and perform that with a batch action.
after that, clear the saved time stamp ensure next time your data won't be messed up.
I'm building a project which is something like an Alarm Clock app. I know there are inherit limitations here (as compared to the built-in Apple Clock app) but I'm trying to assess if these limitations will be a blocker.
In the end, I need to be able to schedule an alarm/notification at a certain time, and have it sound when the phone is locked. Furthermore, I want to be able to interact with the app from the lock screen (including shake and volume gestures).
Does this require me having the app run in the background, and scheduling LocalNotifications? If its in the background, and the LocalNotification fires on the lock screen, is it possible for the User to interact with the application? i.e. With the app SleepCycle, you can shake the phone from the lock screen to trigger a Snooze. Is that sort of functionality possible only because the app is running in the background (in the case of that app, I know it's running in the background)
yes. you need to be running in the background for interacting on the lock screen
I have a question about application background refresh.
In my AppDelegate.swift, I use an NSTimer to refresh my app when my app enters background mode.
On the iOS Simulator, refresh works perfectly in background mode, but when I run my app on the real device, refresh doesn't work.
Is it impossible to use a NSTimer for refresh my app in background?
Your application may not stay long in the 'Background' state. It could go to 'Suspended' or be killed by the system, if it wants to. And of course, no code from your app can run in those states.
That said, I think it's not a good practice to use NSTimer for scheduling background work, and use NSNotification instead.
I recommend to read thoroughly the Energy Efficiency Guide for iOS Apps.
Moves app on iOS geolocates your movements throughout the day even when the app is inactive. In addition to this it also appears to analyse core motion data to identify if you have walked, cycled or taken another form of transport (walking with your phone in a suitcase on wheels reports as a cycle).
What iOS methods and techniques can the Moves app use to continuously capture this data without the application having been open? Geofencing? Background processes?
You can register an app to continue operating in the background. Have a look here at the Background Execution and Multitasking section. http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html