Doing background process even if app is terminated - ios

I want to check the time even if the app is terminated. I have read about Background modes but I don't quite understand it. Can someone give me steps?

Apple has restrictions in place for the use of background operations in an effort to improve user experience and extend battery life. Your app is only allowed to keep running in the background in very specific cases. For example, these include playing audio, getting location updates, or fetching the latest content from a server.
If your task does not fall into these categories backgrounding may not be for you. You may even find yourself with an App Store rejection if you try to cheat the system by using background modes outside the realm of their purposes, so consider yourself warned!
More Info:
https://github.com/NativeScript/sample-ios-background-execution
https://www.raywenderlich.com/143128/background-modes-tutorial-getting-started

Related

Monitor app and lock state changes in the background on iOS

I’m trying to make a focus timer app like Forest, that detects if you leave the app, either by going to the home screen, switching to another app or by first locking the phone and later proceeding to a different app through notifications, widgets, camera etc..
What I can’t figure out is how to monitor such state changes even after the phone has been locked for a while. There are no “background modes” covering this use case, so I would expect the app to get suspended after a while in the background. Nevertheless, apps like Forest do this successfully. Do I need a workaround to keep the app awake, or am I missing some approach that doesn’t require background execution at all?
Help much appreciated!
If I had to guess, it's probably a combination of a few things:
Use the normal app delegate callbacks for app state transitions to determine if the app is still able to execute code. That's the easy part.
You could do something with string and regex processing of console logs to detect when other apps open, close, etc. and other activity which would allow you to detect activity on the device coming from places other than your app.
You need to use some sort of framework that Apple says can get data even in background state. One such framework is Core Motion. There may be others as well that suit your app's specific needs better.
See Execution States for Apps.
See Background Execution.
See Cocoanetics: Accessing the iOS System Log.
See Keep iOS App Awake To Monitor Movement.
See Apple System Log Facility.

Periodically update blockerList.json from a server

I’m building a content blocker app for iOS.
Is there any way I can update the blockerList.json file from a server periodically in the background?
I have no idea how to do this, or even where to start.
If, and only if, a background task improves user experience you can declare an iOS app to have a task that runs in the "background". That is, even when another app is in the foreground, your app could potentially perform tasks "in the background".
However, be very picky about that requirement - often enough, it's not really necessary to do this. In your case for example, you could load the list when your app will move to the foreground, and then periodically while it is in the foreground. Doing things in the background when it is not really necessary will drain battery for nothing - one of the bad habits users hate the most!
Well, lets assume you have good reasons for doing it anyway ;)
An iOS app which has not declared such kind of background task will stop executing shortly after it ceases to run in the foreground. In order to support apps that really have to do something in the background - that is, when this app is not in the foreground - there are a couple of certain "background execution modes" (UIBackgroundModes) which can be used to declare that your app wants to execute such kind of task in the background.
So, the first is to find an appropriate "background execution mode" suitable for your background task and declare that in the Info.plist of your app. (In your case, the suitable mode would be fetch: "The app regularly downloads and processes small amounts of content from the network.")
The next thing is to implement the task and the necessary hooks (uhm, I mean delegates), that is in your case you need to implement application:performFetchWithCompletionHandler. You also need to deal with the "application state transitions".
Here are a few pointers to the Apple docs: https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/
And here is a tutorial: http://hayageek.com/ios-background-fetch/

Keep an iOS application running in background forever

This question might be duplicate of many others but I am still asking because I really need to get any solution of it.
For iOS versions prior to 6, for devices other than those having an A7 chip.
I have an application which interacts with the Accelerometer. I am also running it in background by playing a sound file and turning the Audio services ON for background task.
Now If any third application plays music, it takes control of shared Audio Session of device which causes my background thread to call completion handler assuming that background task has been completed.
Now my application is interruptible by iOS and iOS can kill it if it remains in background for a long time after which I couldn't interact with accelerometer.
Is there any way/trick/hack to keep my application running all the time until unless I kill it by my own.
Again, this is a duplicate question but I really wanted to share my problem with you people to help me in this sense.
Apple is very clear on this:
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 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
Collecting accelerometer data is not listed. Therefore, what you are trying to do is not possible. "Ways, tricks, hacks" tend to get discovered quickly and offending apps removed from the App Store.

How to periodically wake and check status in iOS?

I want my app to periodically wake and check a status, but burn minimal cycles between wake ups. Is the a Core call for this or an accepted design pattern?
Prior to ios7, apps could only ask for execution cycles in background for a handful of purposes (e.g. playing sound or getting location updates). While they added more options in ios7, like periodically fetching data from a server, I'm still not clear there is a way to do this. (It's also a bit difficult for me to imagine why your app cares about battery if it isn't running, but maybe that is my failure of imagination.) Recommend reading Background Execution and Multitasking in the AppleDocs.

Is there a way to prevent iOS6 to kill the specific task process?

I know that iOS has its own task management method and users may never need to care about the processes background. But my requirement is to ALWAYS keep a program alive, it cannot be killed under any circumstance.
Is there a way to do this like "LOCK" or something else function already existed? I'm using iPad4 ios6.01 system.
Thanks
No, that's not possible. (Nor should it be if you think about it. What's to stop every app from saying that it can't be killed?)
The closest you can get is for things like VoIP apps that do run in the background and automatically respawn when they die.
The alternative is to work like every other app: when your app goes into the background you save state so you can restore if it gets killed. iOS 6 even has the state restoration functions.
Short and simple
No, there is no way to make your App "unkillable".
A bit longer
If your App requires to receive location updates while it is running in background, you can use the standard way Apple offers to do so.
Two other options are: your App is an VoIP application (afaik they also get autostarted on system boot) or you're playing audio in the background. Without knowing too much details about how iOS handles such Apps, they might get killed if the iDevice runs out of memory (or the user kills it). But you probably already knew that.
However, as you already mentioned, iOS manages everything on it's own and will kill Apps that run in background to free memory. Additionally as we all know, a user might kill your App at any time using the task switcher of iOS.
And don't try to use the described methods just to run tasks in the background. If Apple finds out about it, your App will be rejected/removed from the App Store quickly.

Resources