How to create an app that functions like demon process at the background?
Is this possible?
Generally not. Apps can't fork processes, the only thing you can do is ask for some time (max 10 minutes) to resume tasks after your app was send to background. For specific use cases (audio playing, location tracking, VOIP) there are well defined exceptions to this rule.
No. Apple only allows specific hooks for background functions, but you cannot - without jailbreaking the device - run anything in the background "generically".
Better think of Multitasking as fast-app-switching with some extras. I recently did a little writeup of this on my blog trying to shed some light on a topic in the Apple discussion forums.
Maybe you find it useful to understand more of the backgrounds. It can be found here: http://www.danielschneller.com/2010/11/ios-421-battery-drain-on-ipad.html
Related
I've been using this app called Moment that tracks how much time I spend on my device every day. I think it's very cool, and at the same time am very curious how it works. I don't have tons of mobile development experience, as I've mainly built web apps, but I'm curious how this kind of functionality could be achieved.
I did a few quick Google searches and found that iOS does have some support for background task execution. The functionality lies in four categories:
Play audio
Receive location updates
Perform finite-length tasks
Background fetch
It looks like I can't do anything tricky using audio callbacks, as Apple will reject the app, but I'm wondering if I can do something with (3).
They do it using Custom Profiles.
This is my very first post on SO!
I'm a beginner iOS developer and I'm looking for a solution for my Swift app.
Is there any way to implement functionality to ask for a server availibilty on a regular basis (e.g. every 5 min) even when the app is not running. That would only require a simple http request so the app would work as a monitoring tool for a specific server. I tried using Background Fetch but it doesn't seem reliable enough since I can't force it to run every 5 minutes.
Is it possible to achieve that in iOS or maybe there are some better ways to implement such monitoring tool?
EDIT:
To partly answer my question I've found this solution that requires the abuse of Apple policy regarding multitasking:
http://yifan.lu/2013/12/17/unlimited-backgrounding-on-ios/
It requires the use of Audio / AirPlay Background Mode, but I can imagine that Apple won't accept such app in the AppStore. Is there any 100% proper way to achieve my goal?
In the WWDC 2015 keynote, it was mentioned that batteries will last one hour longer, and have a low power state that can extend the battery three more hours.
Do iOS App developers have to do anything for this new feature? Is there something their apps must do (or not do) when in that low battery state?
It looks like it has nothing to do with 3rd-Party applications and more about iOS itself. Here's an article I found:
http://www.redmondpie.com/how-ios-9-low-power-mode-for-battery-life-saving-works/
Here's another article that seems to confirm it is only for iOS itself (and background processes like downloading, which may effect some apps):
http://www.idownloadblog.com/2015/06/08/low-power-mode-in-ios-9/
As far as I'm aware, none of this requires any developer interaction. From Apple's perspective that would be a bad idea, since then developers could essentially choose whether or not to comply, and would make their Low Power mode useless (because many developers would not choose to make their app perform worse)
On a side note, I'm happy that iOS 9 will no longer light up my screen if my device is facedown on a table and I get a notification. If you read, there's a few new power-saving features that do not involve the use of the Low Power mode.
It might have to do with the new discretionary network tasks introduced with iOS 7 and other APIs that reduce battery life.
Check out the following sessions from WWDC 2014:
Writing Energy Efficient Code, Part 1
Writing Energy Efficient Code, Part 2
You can either register for NSNotification for NSProcessInfoPowerStateDidChangeNotification
Or use this NSProcessInfo.processInfo().isLowPowerModeEnabled
https://developer.apple.com/library/prerelease/ios/documentation/Performance/Conceptual/EnergyGuide-iOS/LowPowerMode.html
Update for 9.0.2:
I am sorry to say, that the above answer is not correct (anymore).
When in power mode, apps can't react to push messages anymore, because the corresponding function
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
is not called.
I wonder how messaging services go around that?
Has anybody got any clue?
See also my explicit question - with more details on this matter:
IOS9 Push Battery Saver Mode - didReceiveRemoteNotification not called
I have a question about running an app in the background.
I know about how to do it, but Apple does not like the way I'm doing it.
To get you on the same page, I have a security app, and I need to it monitor the device even when it is in the background. It is sort of like a burglar alarm.
I was using background audio mode, thinking it would be okay because I will be playing a sound when it is triggered. Needless to say, Apple didn't like that.
So I added a ping! It pings while active, therefor playing background audio while in the background.
Once again, they didn't like that either.
My app monitors the accelerometer as well (but not always, only when chosen by the user).
My question is, how are apps like Skype, and other similar apps able to turn the status bar red, and stay in the background? (Even some alarm apps will do this, without playing any audio or anything).
Also I can't use the notification system because it does not update fast enough if I'm monitoring the battery level. For my app I need immediate response.
I have also searched around tirelessly for this answer before I posted my question (the answers I have found, do not work for me..) :(
I would greatly appreciate any insight on this, Thanks in advance!
(If you need anymore information, please ask!)
From what I can tell from their documentation, it seems that the only way you can maintain a persistent background connection is by using one of their seven background mode keys, which I can see you've been trying since you registered for background audio. I know some alarm apps as well that use this feature (e.g. Sleep Cycle), and my assumption is that they are also using background audio mode, considering the other six modes are not remotely close to what they would need it for.
I think Apple's reasoning might be that these apps are allowed to do so, because their app is designed to be used when the user is not using the phone actively (i.e. when they're sleeping), and requires the audio to wake the user up, whereas if you are running a security app that wants to be active at all times, it may interfere with other features the user may use like Skype. The red bar will also persist at the top of the device at all times when the user is using it, which they may mistake for something that is still playing since usually when a red bar appears it means to the user that they are still actively using something. Again, I think wake-up alarm apps and others like them that aren't using persistent audio are able to get away with this since they are designed for use when the phone is inactive, so having the persistent red bar when the app is not visible is less of an issue.
In some other cases, like Nike+ (discussed here) and likely pedometers, they seem to be using the location background mode, since they often also track where you went and need to know distance. In that discussion I linked to, it looks like others were able to get accelerometer updates by registering for a background mode that applied to them. Have you tried registering for location movements? One downside I can see to that is it might drain battery life quicker, but if you check location infrequently it might not be too bad? Another is that I don't think you can directly play audio when in location background mode, but you could try to trigger a sound notification? :) That might be a nice workaround for it if that works for your app.
Again, the reasoning I have for why these apps are able to do it is just based on how I've seen other apps operating, and Apple may have different reasons for why it accepts them, but that was my best way of thinking why your app got rejected for using those modes while the others are able to do it. If location isn't what you're looking for, unfortunately I'm not sure from what it sounds like your app is doing that you'd be able to operate it continuously in the background in the way you're expecting.
Being responsive is a need for every app. Users want to have apps which have their content ready when they open it, so developers should use Background Modes to make their apps more user friendly.
Turning on the Background Modes capability
Go to Xcode and open your project.
In your app target, navigate to Capabilities tab.
Turn on Background Modes.
Background Fetch
Background fetch is a new mode that lets your app appear always up-to-date with the latest information while minimizing the impact on battery. You could download feeds within fixed time intervals with this capability.
To get started:
1- Check Background Fetch in capabilities screen in Xcode.
2- In application(_:didFinishLaunchingWithOptions:) method in AppDelegate,
Reference: https://medium.com/#javedmultani16/background-modes-in-ios-3da25b9e6474
You can use background modes only if you actively use the activity that keeps the app awake: e.g. continuously playing audio is a valid use of the audio background key. They'll look pretty hard at whether you really need the access you request, and if not they'll reject you (as you've already found out), as background services drain the battery considerably more than others.
From the docs:
These keys should be used sparingly and only by apps providing the indicated services.
Short story: I think you're out of luck with your proposed implementation.
I have looked through the developer library and found much useful info but still no answer to my question.
The question: Is it possible to have my app run primarily in the background, monitoring the audio route of the phone in general, and reacting to any change in the route with whichever code I choose?
I understand that I must keep within the guidelines of apps running in the background and providing a useful service.
Also note that this app would not have any audio session of its own, it does not need to play or record audio, only monitor the route change (e.g. headset being unplugged).
Hope someone here knows more than I do and can give me a tip!