Swift location updates in background - is it possible to execute method immediately? - ios

Im developing an app and I want to have a feature that sends push notification when user leaves predefined region. For tracking the process of leaving Im gonna use CLCircularRegion and Parse for notifications. The question is - can the app execute methods of sending push when it is in background state? I know that there is a permission available, which allows to monitor location update always and I also know about delegate, which can be used on location update.
The actual question is does it really work when app in background state? The book iOS 8 Swift Programming Cookbook says that location delegate messages are not delivered normally to application, but instead they are delivered in a batch when application again becomes the foreground application. Otherwise, the Apple documentation says that the app becomes awake when location changes.
Say, user terminated app day before and on the next day he left region defined in CLCircularRegion. Will delegate trigger and fire up the method of sending push?

Region monitoring will trigger updates even if the app is in the background. Your app could respond to the region monitoring callbacks didEnterRegion:, didExitRegion: to then trigger the desired push notifications as appropriate.
Per Apple:
In iOS, regions associated with your app are tracked at all times, including when the app isn’t running. If a region boundary is crossed while an app isn’t running, that app is relaunched into the background to handle the event. Similarly, if the app is suspended when the event occurs, it’s woken up and given a short amount of time (around 10 seconds) to handle the event. When necessary, an app can request more background execution time using the beginBackgroundTaskWithExpirationHandler: method of the UIApplication class.
More info here: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html#//apple_ref/doc/uid/TP40009497-CH9-SW1

Related

In swift, send User’s current location to server while app is killed

I am creating a Taxi Driver application in swift. I need to send the driver’s current location to the server by every 30 sec even app is killed. I found the solution while the app is in the foreground or background state. But I am not able to send location to server while app is killed. I tried with VOIP push and startMonitoringSignificantLocationChanges.
When the app is killed and VOIP push receives by every 30 sec, I can get the user’s location but, I am not able to call API and send a location to the server. Can anyone have a solution for sending data to the server while the app is killed?
Using startMonitoringSignificantLocationChanges while the app is killed I am able to send location to the server, but it takes 5 to 10 minutes to get user’s updated location while he/she is driving. I tried physically by traveling from one location to another location so many times. Apple doc tells that location updates when user’s position changes by a significant amount, such as 500 meters. But in reality, it updates by 5km to 7km traveling. Can anyone have a solution for it?
“If your app is terminated either by a user or by the system, the system doesn’t automatically restart your app when new location updates arrive. A user must explicitly relaunch your app before the delivery of location updates resumes. The only way to have your app relaunched automatically is to use region monitoring or significant-change location service. However, when a user disables the Background App Refresh setting either globally or specifically for your app, the system doesn’t relaunch your app for any location events, including significant change or region monitoring events. Further, while Background App Refresh is off your app won’t receive significant change or region monitoring events even when it’s in the foreground.” more...
App refresh is good idea but iOS allows only 35 to 40 seconds in background to wake up your app in background so you should call your functions in this period of time.. here is good example for location based background tasks Usage
and and and apple document says if you use silent push notification. However, the system does not automatically launch your app if the user has force-quit it..refer this

How to open the app in the background after some interval

I am building an iOS application in SWIFT where user location is fetched and sent to the server after every 30 mins. When the app is opened it keeps sending the location lat/long to the server but when the app is closed or suspended it won't send. I want to send the location even the app is closed. Is this somehow possible? I just want to send the location to server no need to open the app again and again?
You app needs to support Background Tasks for being able to run in background.
I believe this has been answered multiple times. Here is the documentation link
Since you are using location, I would recommend you to use UIBackgroundModes as locations.But be sure you don't drain the users battery.
Also usage of background fetch (UIBackgroundModes - fetch) and silent notification can be a great way to get you app running after equal interval of time.
Update
For performing operations you can use silent notification - refer this apple documentation
As per documentation - the system wakes your app in the background and calls the application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
Update 2
For local notification along with scheduling custom action refer this apple documentation. But I really doubt this will help you achieve your desired result. Mainly because you app would get an event only when the user performs any action on your local notification and if he ignores it, you app will remain in background.

Call a web service from iOS at a particular time daily in the background

Is there a way to schedule a function which calls a web service at a particular time on a daily basis?
According to my research, I will have to use local notifications or NSTimer to schedule or call the function at a specific time.
But I need this to work whether my application is in the background at the schedule time (and not running any background task), or in the foreground.
There are two cases:
1. Location service is running in the background in my application
2. No location or background service is running, or the app is in the suspended state (if the user disabled location updates)
But I have to send some status to my server at the scheduled time daily.
You have the following options.
Silent Notification : A silent push notification is pushed from the server, indicating that now is the time, you need to call the webservice and further do the necessary tasks. You will have around 30 seconds to complete the task. Now notice that during this it is not necessary that your app is opened, as this will be done in the background, without your app not opening.
Follow
this
link here to configure your project with silent notification
Location based (Geofencing / Location Change) : Well you manage your app by giving it a location, that whenever it enters/ exists / location is updated near to / on that particular location, the app even if terminated, will be launched in the background.
applicationDidBeginLaunchingWithOptions will be called with a
specific key notifying that this app was launched from a location
change.
Follow this
link to configure your app accordingly. Now the caveat here is this
wont depend on time, but rather a particular location or locations,
whichever suits you.
UILocalNotification or Push Notification : But in both cases, its no guarantee that user will interact with the notification banner and open the application in the first place.
Hence in the first option, you can do the work without the user interacting with the device (Device has to be connected to the internet) on Time based and Location based changes and in the second option User has to interact with the device (notification banner). Please choose the one best for your requirement.

iBeacons and local notifications

I have developed a small iBeacon based application, when the application detects one of our iBeacons makes a call to a web service to obtain a data set and send a local notification to the user. All this is working correctly.
I have now raised the idea that these local notifications could vary over short time intervals, with new content. The problem is that if the user does not leave the region of the iBeacon and reenters, the application will not "wake up" and the user will not receive the new updated notification.
I do not know if this could be solved somehow or actually the approach to make something like that should not be related to technology of iBeacons.
I'm really lost and do not know if anyone would know advise me on how to raise it.
You can schedule local notifications to be delivered at a specific time: use the fireDate and timeZone properties of the UILocalNotification, and then use scheduleLocalNotification instead of the presentLocalNotificationNow method of the UIApplication.
With that in mind you could do this: upon an "enter" event, retrieve a few notifications to be showed to the user over a certain time period while they remain in the zone, schedule them appropriately and let iOS do the delivering.
If the user exits the zone before all the notifications are shown, you can cancel the remaining local notifications using cancelLocalNotification or cancelAllLocalNotifications methods.
In order to do this, you need to get the app to run in the background while beacons are in the vicinity. This would allow you to periodically check for updated content associated with the beacon and then display notifications under two conditions:
When a beacon first appears, and there is a message associated with that beacon.
The message associated with the beacon above changes during the time the beacon is still visible.
As you mention, the second item is a problem, because you need a way to continually check to see if there is updated content despite the fact that iOS will suspend your app within 5 seconds of beacon detection in the background.
A few options, each of which have downsides:
You can use a custom hardware beacon that changes its identifier every minutes or so (e.g. the minor goes back and forth between 0 and 1). This would allow you to monitor two regions and re-trigger on each every minute the beacon is in the vicinity. Downside: This requires building a custom beacon.
You can make your app request an extra 3 minutes of background running time during which you can check for changed messages. Downside: You only get three minutes to display changed messages.
You can specify extra background modes in your .plist so you can continue running in the background. Downside: Apple won't approve your app for distribution in the store unless you have a good reason to run in the background (e.g. a navigation app or a music player app.)
You can send a push notification to your app each time the message changes, which would wake up your app in the background so you could display an updated notification if a beacon is in the vicinity. Downside: Setting up push notifications are a bit complex, delivery can be slow, and is not guaranteed.
Read here for more info on some of these options: http://developer.radiusnetworks.com/2014/11/13/extending-background-ranging-on-ios.html

Why does my iphone app not receive background fetch events after being terminated?

I've built a small ios app which perform periodic updates.
I'm registering my app for background fetches with a callback handler.
When app goes background I'm continue to receive callbacks over the time (every 20-30 minutes, event days later).
Once my app is terminated - the callback handler will be not executed any more.
Is it excepted behavior? I failed to find any mentions about activation from terminated state in Apple documentation on this.
ps: activation from the terminated state is crucial for my app. The approach of the activation could be any, except of user interaction.
It seems that if an app is terminated it will be awaken neither by background fetch nor push notification. No official documentation on this though.
Also keep in mind that if you kill your app from the app switcher
(i.e. swiping up to kill the app) then the OS will never relaunch the
app regardless of push notification or background fetch. In this case
the user has to manually relaunch the app once and then from that
point forward the background activities will be invoked.
Source here

Resources