Start playing audio when app is force killed - ios

I have seen many links that the app will play audio when the app is in background. Using AVAudioSessionCategoryPlayback it is possible.
My question is, lets say the app is killed. but I want to play audio at a particular set time.Even though the app is not in background but force killed. Is it possible?

The app cannot play sound, or do anything for that matter, if it isn't running. You cannot launch the app if it isn't running, because of the bootstrap problem: "you" are the app, and ex hypothesi, you are not running and therefore (wait for it) cannot do anything.
The best you can do is attempt to induce the user to launch the app for you. For example, you could ask the system to present to the user a notification on your behalf at the set time. The system is running (unless the user has turned off the device), so this stands a good chance of working. The user can respond to the notification by launching your app; it isn't certain that the user will do this, but it's a reasonable hope.

Related

Is it possible to build an alarm clock such as the built in app from Apple?

A lot of todo-list apps such as Wunderlist offer some kind of reminder feature which usually just fires off a notification.
But I have never encountered an app that offer a similar notification view as the built in preinstalled alarm clock. You know, that locks the phone and offers a snooze option.
This makes me wonder if the alarm clock functionality that is built into the preinstalled iOs app from Apple including setting time or add a snooze button etc is only available for that particular app?
Since I'm a designer without any coding skills I only want to know if it possible or not.
Unfortunately this is unavailable for developers. You can check all of alarm clock apps in AppStore, all of them are using the standard local notifications. Which will fire once and that's all. You can of course set your custom sound and change couple of options, but it will never work like one build in Clock app inside iPhone.
You can read more about local notifications in Apple documentation: https://developer.apple.com/reference/usernotifications/unnotificationrequest
I figured Alarmy is constantly playing (when there is an alarm set up) a silent sound in the background. Like Spotify, but silent. This keeps the app active all the time. I came to this conclusion by logging the result AVAudioSession.sharedInstance().isOtherAudioPlaying (Swift 4) in my own application. Whenever Alarmy is terminated it returns false, whenever Alarmy is running in the background it returns true. Same thing happens for the app linked by Ernesto Elsäßer.
I used the same technique and I also used this pod to trigger a function when the time comes and start the real music.
I did this for testing and am not going to make an app out of it because playing silent sounds in the background is against Apple's rules. I don't know how Alarmy (or other apps that use this same method) got away with it.
Some related questions:
How do you constantly run in background?
App “Alarmy” is able to play iTunes song from background state… How?
Wake up application in background using AudioSession like Alarmy iOS app
Also you can check this article out.
This app seems to do exactly this, but I have no clue how ...
I tested it, and it managed to start playing music on a locked device at an arbitrarily chosen time, and for way longer than the 30s timeframe allowed for notification sounds. Further, the music is streamed from an online radio station, meaning they found a way to ...
wake up the app from a scheduled notification without user interaction
create a socket connection to a radio station in the background
start playing sound in the background for an indefinite amount of time
I also tried out the alarm in Airplane Mode, and it still played a locally stored backup song, so it probably doesn't abuse the VoiP background mode or push notifications, but really is triggered by a local notification ...
So although I can't tell how, it looks like there is an App Store Review-proof way to create a real alarm app.
(I am not affiliated with the developers of this app, just did some research on what's possible.)

Keep an app active in the background?

I have a volume control app in the iOS store, but one problem that my users have frequently is that the device kicks it for memory control. Is there any way to either force it to stay active (by permission) or to at least alert the user when its no longer active or in danger?
All the answers are under Background Execution in the iOS Developer Library.
Of course, here's the philosophy:
Always try to avoid doing any background work unless doing so improves
the overall user experience.
See Table 3-1 for the types of background execution. There's a category for audio but it requires audio to be played from the app. It does not sound like your app fits in here. So you'll want to look at notifying the user.
You could send a notification to the user when the app falls to background with applicationDidEnterBackground(), or just before it quits with applicationWillTerminate(), supposedly time-permitting.
Running in the background is permitted. I have an app that does it and while the rules have changed and adapting to it has been gut wrenching at times, it is pretty straightforward. In your case, I would think that you could setup to be notified and then just disappear, no? waking from a notification is part of the state changing protocol and you should be able to do that.
As a point of comparison, in Android, background operations like yours would be implemented as services and would have no fear of being terminated. The strangest thing about the way ios chooses to allow background activity is that you are applying for status one time. In Android, I was able to indicate that a single activity should be kept alive when the app is backgrounded, all others could be terminated. There is no way to do that in ios.
I take it my disappear and wait for notifications is probably not going to work for you because there is no way to be notified of volume changes. You must be polling? In which case, you probably are going to have to ask to be kept alive. Would be nice if you could just piggyback on other services, e.g. keep me alive while music is playing.
To answer your other question, yes you will get notified if/when you really are going to get termed, so you could send a notification at that point.

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.

Running IOS App with Screen Locked

I am working on a sports app that uses GPS and OpenEars text-to-speech. The app delivers speed and other GPS info to the user audibly (via earphones) so the user (skier, cyclist, etc) can get the GPS info without looking at the screen. In order to save battery life, I would like the app to run in the background with the screen off. Is this possible with IOS? I tried setting "Required backgrund modes" for both GPS and audio, but my app quits working when the screen is off.
thanks..
One one to get around it is to have a silent audio file playing, when there is no audio, it might get your app rejected in the app store though... here is a question that talks about the silent audio file playing to keep in the background... It might get through app store approval in your situation since it is an app that relays on audio as one of its main functionalities..
You can subscribe to GPS info change notifications and your app will run when something needs to be updated. Check this.
Please note this sentence:
"If you leave this service running and your application is subsequently suspended or terminated, the service automatically wakes up your application when new location data arrives. At wake-up time, your application is put into the background and given a small amount of time to process the location data. Because your application is in the background, it should do minimal work and avoid any tasks (such as querying the network) that might prevent it from returning before the allocated time expires. If it does not, your application may be terminated."
There's no way to do it as service except some special cases provided by ios. But you can simulate it. This post answer how to:
https://stackoverflow.com/a/19121250/2835520
I hope it's gonna help!

Sending Application to background on iphone

Is it possible to send the application to background programmatically on iPhone?
Under iOS 4.0, your app will be put in the background if another app enters the foreground, and there are enough resources to keep your app in the background. You could send a URL to Safari, or another app that has registered for a handler, and hope that there's enough memory (etc.) that the OS puts your app in the background after it starts Safari (or whatever app handled the URL). If you are lucky (which typically happens fairly often), you will have sent your app programmatically to the background.
Of course, whether your app runs in the background, or is just suspended, depends on other things (which you have to register with iOS 4.x for).
Apple does allow apps to exit (kill themselves) programmatically. But it's meant only as a last resort for when something bad happens from which your app cannot recover.
On all other occasions, apps are expected to stay in the foreground and running until either the user presses the home button or the app opens another app, with the users consent.
Actually you don't want to kill the app, however moving it to the background and returning to the home screen would leave a very similar impression. That's why I don't think Apple would approve it and consequently doesn't offer, as far as I know, any means to move an app to the background programmatically without opening another app.
No it is not possible. Only iOS can put your application in the background.
Note that there's a lot of confusion between "background" and "inactive". An inactive application is one that is not displayed on the GUI but is still running.
Technically an application in the background isn't running; it's dormant. Depending on the mode used, a background applications can receive signals and "wake up." See Background Modes for more details: https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW22
But a background application can also get terminated at any point without warning. As a matter of fact I always treat a background application as terminated. Therefore you should always clean up your application before you enter the background, and re-initialize it when you enter the foreground.
This is a must-read on this topic: https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
check out these post
local notifications?
iPhone - Backgrounding to poll for events

Resources