I want do code an alarm-clock.
Now the alar-clock should play a music-file at a specific time.
But I don't know how to call the method with AVAudioPlayer while the app is in background?
Can you please help me I want do this for the first time.
thanks :)
You should not use a long-running background thread in order to trigger a sound at a certain time. A running thread prevents the device from going to sleep, greatly increasing power requirements.
Further, Apple greatly restricts the amount of processing you are allowed to do from the background. You can request time to complete a long task from the background, but only a short period of time is allowed. (A few minutes at most if memory serves.
You should probably create a local notification instead. The local notification will display a banner message on the lock screen if your app is not running, and optionally play a sound that you specify.
Take a look at the UILocalNotification Class Reference in Xcode for more information.
Related
What are the different options for running code on an iOS device at a specified time while the app is in the background? So far I have found that I could possibly use a Timer object, use the Grand Central Dispatch timer, and use CloudKit Push Notification with the Apple Push Notification service.
Any ideas are welcome. I'm having a hard time finding the solution.
Here is the apple documentation for background execution.
Here is the apple documentation for the execution states of an application.
You should understand a few things:
When an app moves into the background you have a very small amount of time (3 minutes approximately) to execute finite-length tasks.
If your application is not running, you could use a notification to wake the application. This requires the user to take an action and will make the application active.
You can also wake an application with a local notification, it doesn't have to be a remote notification.
The code that you want to run really needs to fall into one of the blessed scenarios that apple defines otherwise you run the risk of being deprioritized or ignored entirely.
A block of code scheduled to run with a Timer or GCD will not be invoked while the application is in the background.
Roughly, you should register a background execution mode and follow the guidelines I linked above and while your application is backgrounded the application will be given time to execute code.
I created an app that will send text messages through an API similar to Twilio, and I have an NSTimer that fires a function every set amount of time. The problem is that if you press the home button, it will totally stop the timing, which defeats the purpose.
I have read that it is possible to have your phone play a mute sound until closed to make sure it still runs in the background, but I can't find a resource on how to do this. Here is an example: To run NSTimer in background
If anyone has any information on how to do this, I'm sure it is very simple, and I very much appreciate the help.
Thank you.
If you set up your app as a background sound playing app then yes, it is technically possible to play a "silent sound" in order to keep it active in the background, but your app will be rejected by Apple if you do this.
Apple expressly forbids what you are trying to do.
You should look at using scheduled local notifications. Those will alert the user, who can bring your app to the foreground and let it perform the desired task.
I'm struggling with what's probably a basic iOS concept.
My apps needs to set an alarm for a specific time (10 mins in the future, 30 mins in the future, etc). I need to execute a line of code (and access a sharedInstance in my app), when that delay expires.
Local Notifications sound like the right tool, but I do NOT want to notify the user. I understand there's a callback feature, but I'm not sure if it's the right way to do what I want. It almost seems like abuse.
Any comments? How do you schedule a block of code to be run, in the future?
Edit: I need background support, or at the very least I need this to work when the screen is off.
You cannot accomplish this locally. You need to look at remote notifications. Remote notifications are silent push notifications which give your app time to run in the background. This solution will only work on iOS7.
Local notifications should be used specifically for notifying the user of something (when the app is in the background).
When your app is running you should use an NSTimer to schedule future execution.
An alternative if you love GCD and blocks would be to use dispatch_after(dispatch_time(DISPATCH_TIME_NOW, #TIME# * NSEC_PER_SEC), dispatch_get_current_queue(), ^{...
If you're playing music then you get an opportunity to run in between each song (or is it when the buffer empties, I can't remember) and you can stop playback at that time. If you store an NSDate of the time that you should turn off then you can check against it each time you get to run.
When iPhone app is in background state or when app is minimized, I want to monitor the duration of time the app is in background and on every regular interval like every 24hrs need to get updates from web server and write the modifications in my local database.
This monitor cycle will repeat as long as app is in background. Is this kind of task is possible in iPhone applications ? Could any body help me how can I perform these tasks when app is in background state ?
beginBackgroundTaskWithExpirationHandler: is the API to make the app in background for long tasks but how long will this support.
Background tasks are killed after 10 minutes time, so to answer your question: No, it's not without faking some kind of long term event handling through a significant location's update, and even then, you have no control over when exactly the events are sent. On the other hand, using APNS to alert the user to a change has the possibility of being ignored, but comes with regularity.
I have a background app with a UIBackgroundMode of location.
I would like my app to additionally contact a server every few or several hours to see if there is some new data for it (because using apple notification push would notify the user and that is not desirable).
Polling is something I would never use on any other OS, but with iOS they don't leave you much choice if there is certain functionality you would like to try to achieve.
If the polling interval is quite lengthy such as a few or several hours between polls, and the polling activity itself only lasts several seconds then the usual knee-jerk reaction about it draining battery life is greatly diminished.
Would a repeating NSTimer fire when an app is in background mode? If not is there another type of timer or mechanism available?
If it's just to check for new content, and not really time sensitive, you COULD use the significantChanges background location method...but if the user stayed fairly immobile it'd rarely/never fire. I would probably also add the update check in applicationWillEnterForeground to be more sure
No, that's not allowed. You should have a look at Push Notifications and find a server side solution.