Transparent Local Notification / Callback - ios

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.

Related

make timer run on background iOS for more than 3 minutes

So I need this app to run timer for more than 3 minutes, and play a sound like very 45 seconds, most of the solutions here are just for less than 3 minutes on iOS. Is there a way to make it run all the time, unless the app stops?
After the research, I guess the solution is implement beginBackgroundTaskWithExpirationHandler
and stops it by giving a handler and set location update on plist?
As for audio, besides setting the plist, anything else need to do to play audio?
Swift how to use NSTimer background?
iphone - NSTimers in background
Run app for more than 10 minutes in background
No, in order to save all of us from apps that kill our batteries, Apple does not allow apps to continue to run in the background for more than a few minutes. There are only very special limitations (music playing apps, VOIP, navigation apps, etc.) which permit ongoing operation. In terms of details, this is described in About the Background Execution Sequence.
If you want to notify user of something at some future time, you can use local notifications. When you do this, though, you don't control whether your app restarts or not. Only if the user taps on the notification (assuming they even granted your app notification privileges at all), will the app be reopened. For more information, refer the the local notification discussions in UserNotification framework documentation. But note, this is not intended for alerts every 45 seconds, but rather for a significant notification scheduled for some future time.
For discussion of how one might marry local notifications with timers, see swift NSTimer in Background and this follow up question swift calculate time for timers running in background.

swift/ios refreshing app data when in background

I'm writing a iOS/Swift application which reads data from a REST service each X minutes and updates the UI accordingly.
Now I would like that when the app is put in the background, a task keeps being invoked at X minutes intervals reading from the REST service and, in case the data just read satisfies a given condition, show a notification prompting the user to bring the app back to the foreground.
In my searches I've read that during applicationDidEnterBackground event, I should start a task with beginBackgroundTaskWithExpirationHandler.
The problem is that, if I've understood correctly, this allows a maximum of 10/15 minutes after which the app is terminated if the task is not stopped with endBackgroundUpdateTask, while I want the task to keep polling the service indefinitely (at least until the user disable it from the app's settings)
My question is:
How is this kind of functionality performed normally? Do some common solutions or best practices exist for the solution of such a problem?
Use iOS Background Fetch feature where you can specify minimum background fetch interval. But actual interval between successive invocation of your code will be determined by iOS framework. For details checkout this link: http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch--mobile-20520
I use this approach in my app and I think it is a preferred way of doing.
You can use a local notification that can be presented from the background in case your condition is met.
Correct, iOS will eventually shut down the background process, you can't enforce continuous background activity. Use the backgroundTimeRemaining property to check how much time your application has left and try to handle it as gracefully as possible by calling endBackgroundTask so that iOS does not force kill your app.
As a solution, you could think about using remote notifications with with content-available : YES, which runs the didReceiveRemoteNotification
Have a look at the Parse.com Their local datastore is an abstraction for what you are trying to acheive.
By the way, is it really necessary to refresh in the background. If call is relatively quick, there is no need to refresh until the user open's the app. Background processes like that, using the net can be quite battery consuming when the user are not on a Wifi. So consider the use case carefully!

Consistent background fetch in iOS7?

I was just wondering if it was possible to consistently update the data for my app every 60 seconds.
I have the following code right now:
NSTimeInterval testTime = 60.0;
[application setMinimumBackgroundFetchInterval:testTime];
However when testing on the simulator, the app initially grabs the data upon install, and then doesn't do anything else. My web searches tell me that iOS determines when to actually trigger background fetch.
That being said, is there anyway to consistently have background fetches occur for the user? Like say, for every 1 minute? My app's big selling point depends on the latest up to date information. I think it's possible, as apps like Gmail,twitter, SnapChat are always checking for new data...
Thanks
There is no way to achieve this using background fetch. You can only force this behavior using push (silent or otherwise), sending a push message to each device once every period of time.
Allow me to say, this is a terrible design. Polling is a terrible design for mobile apps. You should implement proper push notifications, notifying the user of new content, and loading it in the background as the OS deems possible.

iOS Background Fetch mode can be used to schedule some operation in the future that doesn't actually fetch remote data?

I'm doing an app that requires to reschedule local notification on daily basis. I'm aware about the repeatInterval property, but repetitioon here is like each 2 days etc.
I've seen silent notifications, but they can be used only with push notifications and due to some requirements I can't use that approach.
Now the app works on the hypothesis that the user will open the app quite enough to reschedule those notifications. This hypothesis is fine and we all agree that will work, but I will be more confident, if it would be possible to reschedule them on daily basis without opening the app.
I've seen the new API Background Fetch, this could be really good for me, but from doc and WWDC videos I didn't understand if it is possible to use for "everything" or just to fetch remote data.
As I understand you can do everything that does not take too long, plus you don't get a guaranteed interval when you app will be woken up - you just can request a certain minimum wake interval, which will be treated as a suggestion by iOS.
The only catch is that apparently you have to create an NSURLSession and actually do a web request, upon which return you can do whatever you want. So, you can do a dummy request and forget about any data you get returned, or maybe even create a failing request, as you are not interested in any real request at all - although I'm not sure what Apple will do when you implement the latter ...
You can use repeatInterval property of localNotification
localNotification.repeatInterval=NSWeekCalendarUnit;
For daily basis you can use NSDayCalendarUnit
No there are not API which can wake app after some time interval.
In back ground fetch mode application will wake up after you set minimum time. Application will wake up any time after minimum time interval might be after 5 min or 1 sec. and that will decide system on application usage. so we can not take this approach, if you willing to retrieve remote data compulsory after some time interval.this may help u.
http://www.doubleencore.com/2013/09/ios-7-background-fetch/

Can a background app (with a location UIBackgroundMode) use a timer to poll a server every few hours?

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.

Resources