Building a background app with audio and location services - iphone

I'm building an app that is location-aware and triggers text to speech whenever the user gets near a certain point. I have singleton classes GPSManager (for getting location updates) and RouteDetails (for defining the points that should trigger the text-to-speech). I also have a view controller that shows a map updating the user's location from GPSManager and showing a route from RouteDetails.
What I want to do is keep the functionality of the view controller running when the app is suspended, i.e. be notified when the location is updated, compare it to the locations in RouteDetails, and then play the text-to-speech. However I can't seem to find a good example of this being done - how do I made sure RouteDetails and GPSManager stay in memory? And where should I put the text-to-speech module where it can access the details in RouteDetails and also be in memory so that it can play when the app is suspended and the phone is locked? I'm assuming the view controller can't receive notifications when the app is suspended.
Also, I've added the location and audio options to the UIBackgroundModes key in the Info.plist file. And I don't need the app to bring itself back into the foreground when it gets a location update/plays a sound, it just needs to play a sound and update a variable in RouteDetails.

Related

iOS - accelerometer in background shows location services popup, but we don't need location services

Our app would like to access accelerometer data in background. A possible way to achieve is to use Core Motion for accelerometer readings -
with
CLLocationManager* locationManager;
CMMotionManager* motionManager;
...
[motionManager startAccelerometerUpdatesToQueue: ... withHandler: ...]
which works fine on the foreground, but the only way I've found to receive the updates in background is to set the app to allow using Location in background, and call
[locationManager startUpdatingLocation]
in -applicationWillResignActive:
The problem is, when I call startUpdatingLocation that a window pops up with text Turn On Location Services to Allow "app" to Determine Your Location.
But of course, I receive accelerometer readings regardless of whether Location Services are enabled, but the popup is annoying and will probably confuse users.
Is getting accelerometer data in the background somewhat tied to attempting to receive location updates?
You can use startAccelerometerUpdatesToQueue in background, the only thing you need to meet — use any background mode to make you app running in the background (location updates (your case), playback, VoIP or BT4 central).

App launched from the first screen from the foreground ios

When I pressing "home" button, app calling - (void)applicationWillResignActive:(UIApplication *)application, ok its good, but when I wait for a some time (10-15 mins), app starting from first loading screen, why? I have added:
[UIApplication sharedApplication].idleTimerDisabled = YES;
But not success. I think it goes to suspended mode, how do I prevent this? Thanks.
It means that your app has been killed by the system.
You can't prevent it completely, the system kills background apps when it needs more memory, but you can do something like this:
free as memory as you can when the app goes in background: doing this, when the system needs memory, your app won't be one of the first to be killed because its footprint is low.
Since you can't be sure that your app won't be killed, use state preservation / restoration: Link
EDIT:
since your app needs to reproduce audio in the background (as written in the comments) I give you some extra advices.
remember to register your app background mode (from this link: Multitasking guide) --> "by including the UIBackgroundModes key (with the value audio) in its Info.plist file"
the app continues to work in the background since it's playing audio, but when the audio is suspended the app is suspended too (it means that it can be killed). So, you must use state preservation to restore it when the user comes back.
Another extract:
"When the UIBackgroundModes key contains the audio value, the system’s media frameworks automatically prevent the corresponding app from being suspended when it moves to the background. As long as it is playing audio or video content, the app continues to run in the background. However, if the app stops playing the audio or video, the system suspends it."
You app has exited. If you want it to come back to where you left off you'll have to preserve the state somehow, check on launch, and then restore the state.

Responding to events when device has been locked

I'm working on a radio alarm clock, and i have some issues.
I am using local notifications for the alarms, so it has a gentle fallback if the app is not running.
I am well aware of the limitations of the device, and i know what i can and cannot do when the device has gone into background.
But my question is this:
I have seen other apps starting an audio streamer when i've locked the device. How is this possible? May this be inside an execution-timeframe?
How is the best way to implement this? Is it any way i can activate a streaming session when the device is locked?
Edit
To clarify: I know how i make audio play in the background. But the issue is triggering the audio-playback when an local notification or some other event fires.
One app that seems to do this, is Radio Alarm Clock. I haven't tried it for long period of times yet. But it seems to do this. A video demo of the app: http://www.youtube.com/watch?v=KJQiFOcdBWk
Have you already declared your background task?
Support for some types of background execution must be declared in advance by the app that uses them. An app declares support for a service using its Info.plist file. Add the UIBackgroundModes key to your Info.plist file and set its value to an array containing one or more of the following strings:
audio — The app plays audible content to the user while in the background. (This content includes streaming audio or video content using AirPlay.)
iOS App Programming Guide - Implementing Long Running Background Tasks
You can add this by clicking on your main project icon, then under the Info tab you can add "Required Background Modes" to the "Custom iOS Target Properties" section. "App Plays Audio" will be one of the three default values.
Big Edit With New Answer:
If everything else is already in order, you can keep your app running in the background using the UIApplication method
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler
detailed here: UIApplication Class Reference
with an example here: Hour 21: Building Background-Aware Applications
This allows you to run an instance of NSTimer which triggers your music player. The difference between this approach and UILocalNotifications is that this method never lets the app fully enter the background mode, the music player exists the entire time which subverts the need to create it from the background, which looks to be impossible.
There may be limitations to how long of a timer you can set, I haven't tested this past 14 minutes out.

view capturing in background

we can easily capture a view(screenshot) programmatically in iPhone.
But i am trying to capture screen automatically after every 5 seconds.that also, can be done.
But my main objective is , to capture screen, even if the app is closed, i.e in background.
Apple doesn't allow to background process to run for a long time, but can we do it for 5-10 min in background.
Capturing screen,even if the app is closed.Ofcourse user can close this function, within the app.
Apple might reject this, but is this possible ?
Regards
This is possible but it's not easy. You can register you app (via a .plist setting) as being a media app, this will allow it to run in the background. This is normally to allow media apps to continue playing music when the app is sent to the background. However if you are careful you can get it to do other thing in there too.
In your plist under "Required Background Modes" add a key "Item 0" and set it to "App plays Audio". This will mean you can do it.
You will then have to using AVAudioPlayer, play a silent sound that is say 5mins long, and then register one of your controllers for the AVPlayerItemDidPlayToEndTimeNotification notification. Then when this calls back, you can have your bit of code that, first takes a screenshot, and then starts the sound again.
This is the general concept, look somewhere like :
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_background-audio/
for how to set up the audio playing.
Just a note: APPLE WILL REJECT THIS!
If you are making an app for personal use only, this is fine, but will never make it to the app store.
Hoep this sheds some light on your situation! :)

applicationWillTerminate and background: terribly confused

I understand that applicationWillTerminate is no longer called in iOS4, practically. But I have this situation:
my audiobook goes in background mode and keeps playing audio;
until version 3.x of SDK I saved the point where one listened to the MP3 file, in applicationWillTerminate;
now I was told to keep this saving in applicationWillTerminate (for iPhone 3G) and to implement the same method in applicationDidEnterBackground: but this saves NOT the point where user was at the end, just the point when he entered the background...
But, which is the moment, or better the invoked method, where user exits the application from the application dock? Does this exist?
ApplicationWillTerminate is called when there is a memory problem, but really I can't figure what happens when the user himself shuts down the app.
I repeat: applicationDidEnterBackground does not help me because when the user enters in background mode, he can stays in this position for a long time listening to the audiobook and when he starts again the app, after this is shut down, I mean, he will find the position of the audio file when he entered the background mode…
I'm really confused…
Thanks for your help!
You should save the play location automatically in applicationWillResignActive:, applicationDidEnterBackground: regardless of whether the audio keeps playing or not. Then put another save in applicationWillTerminate. That way the last play location is saved regardless of what happens next. The next event simply overwrites the saved play location to update it.
An even better option would be to have the audio player object itself trigger the save whenever it is interrupted e.g [AVAudioPlayerDelegate audioPlayerBeginInterruption:] or similar. It might takes some extra work but it would guarantee that the play location was always saved regardless of the cause of the interruption.