Prompt user when turning off location service from Settings when app is not running - ios

I want my app users to keep Location Service Turned ON all the time. This is the requirement of the application which will keep checking user is on the job location even after app is not running or killed. I know this can be achieved by enabling Background Mode : Location Updates. I have referred the Location updates which help me getting updates even after app is killed.
Now, I want to notify users when they turn off Location service if app is not running.
How can i determine the status of Location Service after app is killed/Not running ?

Related

How to run Timer Repeatedly in Background && Suspend iOS App

I am new to iOS developement and How can i Run timer Repeatedly(To call function) in Background && Suspend ?
I want to Run Timer Repeatedly,Then I can Get the user current location Repeatedly.
Also is there any other method to get user current location using notification or Timer.
The significant location change service delivers events normally while
an app is running in the foreground or background. For a terminated
iOS app, this service relaunches the app to deliver events. Use of
this service requires “Always” authorization from the user.
The region monitoring service delivers events normally while an app is
running in the foreground or background. (You can use this service for
both geographic and beacon regions.) For a terminated iOS app, this
service relaunches the app to deliver events. Use of this service
requires “Always” authorization from the user.
Read this apple's documentation -
https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/doc/uid/TP40007125-CH3-SW73
If you want user's location in active and background state you need to do following
Select your project using the project navigator, then find your app’s
target and choose the Capabilities tab. You need to enable Background
Modes, then check the box marked “Location updates”.
Please keep in mind that you can get user's location in background mode only when user allow your app to access device's location Always
Here is very nice article on how to get user's location seamlessly while app is in active and back
https://www.hackingwithswift.com/example-code/location/how-to-read-the-users-location-while-your-app-is-in-the-background

I am not getting user current running location after Terminate the Application

I require the user running location and route map path, When an application is Terminate or Background mode.
Currently I am using significant location for application kill mode, but we are not getting proper distance and route.
Because didUpdateLocations is called after some specific interval so that I am not getting proper distance.
Please guide.
According to Apple docs :
Apps can expect a notification as soon as the device moves 500 meters
or more from its previous notification. It should not expect
notifications more frequently than once every five minutes. If the
device is able to retrieve data from the network, the location manager
is much more likely to deliver notifications in a timely manner.
This is why startMonitoringSignificantLocationChanges() would not work reliably for tracking user location change other than driving/public-transport.
Now, coming back to your problem of having to track user location when app is terminated, unfortunately you cannot track location when the app is terminated
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 the significant-change location service.

IOS Getting location updates when app terminated without using significantChange

I apologise for the redundancy of this topic, but in spite all the given answers, I can't identify the possibility of getting accuracyBest location updates when the app is terminated.
I don't want to use monitoringSignificantChange, I want the best possible accuracy; I won't submit the app on the AppStore, so Apple restrictions are not a problem either.
I have gone through these:
-Location update even when app is killed/terminated
-iOS update location even when app is terminated
-Working with location updates when app is terminated
-http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended
and many more, but it is not clear whereas it's possible or not. I currently have my project, everything works great with significantChange but I now need better accuracy.
Could somebody tell me straight forward if getting best accuracy location update when app is killed is possible please ?
Thank you infinitely,
Looking at the below content from the Apple doc, you clearly have 2 alternatives against using significant location changes to wake a app from the background. I have marked in bold the services you can use to relaunch the app if it has been terminated.
Using Location Services in the Background Most location services are
meant to be used while your app is in the foreground but some can also
run in the background. In some cases, location events can even cause
your app to be relaunched to process an event. To run most location
services in the background, you need to enable the location updates
background mode in the Capabilities tab of your Xcode project. For
services that launch your app, you need to request (and be granted)
“Always” authorization from the user.
The standard location service delivers events normally while an app is
running in the foreground. When your app is in the background, this
service delivers events only when the location-updates background mode
is enabled for the app. This service does not relaunch iOS apps that
have been terminated.
The significant location change service delivers events normally
while an app is running in the foreground or background. For a
terminated iOS app, this service relaunches the app to deliver
events. Use of this service requires “Always” authorization from the
user.
The region monitoring service delivers events normally while an
app is running in the foreground or background. (You can use this
service for both geographic and beacon regions.) For a terminated
iOS app, this service relaunches the app to deliver events. Use of
this service requires “Always” authorization from the user.
Beacon ranging delivers events normally while an app is running in the
foreground. When your app is in the background, this service delivers
events only when the location-updates background mode is enabled for
the app and the standard location service is running. (If the beacon
region’s notifyEntryStateOnDisplay property is YES, waking the device
causes the app to range for beacons for a few seconds in the
background.) This service does not relaunch iOS apps that have been
terminated; however, you can be relaunched by monitoring beacon
regions using the region monitoring service.
The heading service delivers events normally while an iOS app is
running in the foreground. When your app is in the background, this
service delivers events only when the location-updates background mode
is enabled for the app and the standard location service is running.
This service does not relaunch iOS apps that have been terminated.
The visit service delivers events normally while an iOS app is
running in the foreground. When your app is in the background, this
service delivers events only when the location-updates background mode
is enabled for the app and the standard location service is running.
For a terminated iOS app, this service relaunches the app to deliver events. Use of this service requires “Always” authorization from the
user.
Enabling the location-updates background mode ensures that an app
continues to receive location events while in the background. When the
app moves to the background, the system adds the location-services
indicator to the status bar to let the user know that an app is using
location services. The system may still terminate the app at any time
to reclaim its memory or other resources.
Also from the doc,
Getting the Visited Locations
In iOS, the visits service provides an alternative to the significant location change service for apps that need location
information about interesting places that the user visited. For
example, if the user is in one location for an extended period of
time, the service might generate an event when the user arrives at
that location and another when the user leaves that location. The
service is intended for apps that might already be using the
significant location change service and want an even lower power way
to do so. You would not use this service to create navigation apps or
apps that rely on regular location updates.
Document Link:
https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/doc/uid/TP40007125-CH3-SW73
Continuous location updates stop when your app is terminated.
Geo-fence notices will re-launch your app even if it's not running. I think a significant location change will also relaunch you're app but I'm not as sure about that.
Once you're relaunched you can start location updates again with the accuracy set to best like you want, but I think you're going to need to use one of those two APIs to get relaunched again after you've been terminated.

Being notified when user turns on location services iOS 7 and up

I am faced with a situation where we would like to kick off a particular process based on whether or not a user has locations services enabled or not. This part is trivial. However, we want to extend this functionality by changing our process if the user turns the locations services off or on. We currently are able to monitor, and change or process, when the user turns off location services. My question is, how can our app be notified when the user turns locations services back on?
Here is what we are currently working with
iOS 7
Background Location Mode is enabled
We are using request always for location services
Here is what we tried
We have tried setting a timer after a user turns off the location service to periodically check if the user turns back on the location service. This is less than desirable for many reasons. Besides that the timer is killed once location services is turned off because background services is no longer needed and the OS kills our process. This is understandable.
We also went the approach of notifying the user that they just stopped a process and they need to turn location services back on. There are two problems with this approach.
If the user does not have notifications enabled then this approach is no good at all.
If they do have notifications enable then we first need the user to turn the location services back on and then they would need to open the app back up to get our app to "wake" back up.
Thank you in advance!
After hours of research and implement trying to a solution we have discovered that this is not possible as of iOS 8. For reference here is what we found:
We were able to be notified when the user had shut off the location services, ONLY if they shut off location services to our app. We did not receive a notification if the user shut off the main location services.
We did not receive a message if the locations services was turned back on, regardless, if it was our location service or the main location service.
You cannot use a timer approach because the timer is killed if the app is sent to background. So we could not have periodically checked. This would not have been acceptable for our use case but it was worth entertaining
You can still call CLLocationManager as if you had permission to use location service, even if you don't have permissions.
If you do call the CLLocationManager you will start receiving location messages, after permissions have been granted to your app, without having to re-register to receive location messages.
Our solution:
The combination of 4 and 5 was our saving grace. We were faced with an edge case where a user could have been eligible for a location event trigger but previously had denied use the location services and then after becoming eligible the user grants us permission to use location services. After doing a little refactoring we moved our location manager so we would register for location event updates, specifically geofencing, even if we did not have permissions. After testing we found that in the edge case event we started receiving location updates even without having to reinitialize the location manager. The same applies to beacon region monitoring for anyone interested.
Just check your location services after app has entered foreground.
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterForeground), name: UIApplication.didEnterForegroundNotification, object: nil)
func applicationDidEnterForeground() {
print("Location services enabled? \(CLLocationManager.locationServicesEnabled())")
}

Does location update remain running after force close app

I am sending user location at n time on server for tracking. Working fine in foreground and background mode. But my question is bit more theoretical.. Does my location update service remain running after i force close the app on the device?
I know i can check it on server, But sorry i don't have server access permission. So i have to be sure in which scenario my location service will stop running.
Yep it will continue to be updated when the application is sent into the background. For some official docs check here under the "Getting Location Events in the Background (iOS Only)" section.
Also, under special circumstances you can register your application to have a true background process running like Android allows. So when the user kills the background application, your app doesn't really die. The background process continues to work. Apple will only allow apps that have a necessary reason for this though. Such as a turn by turn navigation app (example from the docs).
Yes, if the application is removed from background then location update will be stopped.
iOS will only restart an app after a force close if its uses region monitoring or the significant-change location service. From the iOS Location and Maps Programming Guide section on Getting the User’s Location:
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 the significant-change location service.
iOS only restarts the app if it has Background App Refresh enabled:
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.

Resources