I have application which is sending tracked data to our hosted server whenever location manager called in background. Its working fine normally.
But issue is when user switched off the iPhone and turn ON it back application is showing GPS icon but application is not giving response.
PS: User have not touch the app or start after restart the app.
Is it possible to get the response from the app after restart the device?
That is because the app is not active after restarting the iPhone. You could add the voip key to UIBackgroundModes as detailed in this SO post (and a sample app on GitHub) to start your app again after restarting the device. However, if you use this app only for tracking the location, Apple will likely reject your submission for making use of the voip-key.
If you use region monitoring, then your app will be automatically started in the background when a user enters or leaves a region, even if the device is turned off and back on. Use region monitoring, see this answer on SO.
From the Core Location documentation:
The regions you register with the location manager persist between launches of
your application. If a region crossing occurs while your application is not
running, the system automatically wakes up your application (or relaunches it)
in the background so that it can process the event. When relaunched, all of
the regions you configured previously are made available in the
monitoredRegions property of any location manager objects you create.
Unfortunately, this is unclear as to whether or not the same app will also be relaunched after device reboot in response to a region change.
Will an iOS app that monitors significant location changes in the background still be relaunched, even after a device reboot?
Yes, your app will be launched to respond to the region events even if the phone is restarted, and even if the user did not explicitly run your app after the reboot.
I haven't seen any documentation that spells that out clearly. But this is how it works in a couple of my own apps.
Just to make an update. I know that this is an old question, but I want to confirm that it works on iOS7, even after a reboot.
Check my question here:
Are background mode location and fetch called even after the device restarted in iOS7?
Is it possible to wake up an app every x minutes, so that the application can do something in the background? So the app stays in the background.
No, such behavior is not supported by the iOS SDK.
This doesn't wake an app up every x minutes, but will launch an app into the background:
An app can be woken by a significant location change, if the app has indicated that it wants to monitor such events.
See: CLLocationManager Docs
Look for a method called startMonitoringSignificantLocationChanges. If a significant location change occurs while your app is not in the foreground or isn't running at all, your application will be launched in the background, allowing the app to perform background-only operations (e.g. no view code will run).
You cannot wake your app programmatically. You could incorporate push notifications; however, the user would have to opt to open the notification.
No, you can use internal (like reminder) or external (push) notification to to open an app. however, the user would have to opt to open the notification.
I am doing a tracking kind of application for internal use of an organization and do not wish to submit it to app store.
What I am doing in the application is I am tracking the phone calls, messages etc.
My app runs in background once I start the app manually and keeps on running in the background until I close the app or the phone is switched off.
The thing that I want to add to my app is, I want to load the app automatically when the phone is switched on again.
Any idea or guidance will help. Suggestion for use of private apis is also welcome.
In private api, in file SBApplication.h there are all methods that you need.
In particular:
[...]
-(BOOL) _shouldAutoLaunchOnBoot:(BOOL)boot;
-(void) autoLaunchIfNecessaryOnBoot:(BOOL)boot;
-(void) _cancelAutoRelaunch;
-(void) _relaunchAfterExit;
[...]
etc, etc...
hope this helps.
As far as I can understand you can do it by registering your app for significant location changes. If an app registers for significant location changes, as soon as your cellular phone moves to a new tower, app receives an update. If the application is suspended when an update occurs, the system wakes it up in the background to handle the update.
So if you close the app and turn of your phone, as soon as your phone will restart it should get an update and it will force your app to run in background mode.
For more info read iOS programming guide: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html
Hope it helps
Is there a way to manually tell the device to dispatch a significant location change notification that will wake up any applications registered for this notification?
This is for testing only, and I realize this private API call would get rejected upon submission to the app store.
So two answers:
A) Hop on a train :-)
B) Use the simulator. In iOS 5.x simulator, there is a debug menu that has a location submenu. Choose freeway drive. This will start the simulator on an imaginary journey down the scenic 280 in Northern California. It gives you everything but the view: your app will get Significant Location Change updates, and will also be launched in the background if it has been suspended.
To verify that you are actually moving, launch safari in the simulator, and go to maps.google.com and click the little track my location button. You should be moving.
Awesome! Now how to debug the lifecycle problem of being launched by the system? Easy! Have xCode wait for your app to be launched to start debugging. From the Scheme menu, choose edit scheme. In the Run Scheme, and the Info tab, for the "Launch" setting choose : "Wait for My.app to launch".
Run your app once in the simulator, so that it starts monitoring for location updates, then force quit it, so that it is suspended. Add a break point in your application did finish launching function, and wait. As soon as your simulator has gone far enough, your application will be woken up, your breakpoint hit, and you are in the money.
But really, the train ride is more fun.
Well, I've found that I can do this by toggling on and off Airplane mode and/or WiFi. Perhaps start the app with the device in airplane mode, then close the app and turn airplane mode off. That will turn the GPS on and force a location update to be dispatched.
I also wanted to test the relaunch of my terminated app which uses significant change monitoring. I wrote a piece of code so that it would display a local notification when it gets launched by a location key in the launch options dictionary.
I ran my app in the simulator. Then killed it from the multitasking bar. Then I set the location of the iOS simulator to a custom location. I quit the simulator and started it again. My app received the significant location update and showed the local notification.
I was struggling with the same issue, how to test 'startMonitoringSignificantLocationChanges' and check if my app is receiving location updates when suspended.
I couldn't manage to catch the execution on a breakpoint but I managed to see the results of my implementation working by sending the new location data to the server.
The whole flow:
- Implemented with 'startMonitoringSignificantLocationChanges' and an API call to my server to update the location latitude and longitude
- Set the location updates background mode capability to true
- Run the app so the location manager is initiated and the app is listening for location changes
- Force closed the app
- Set the debug->location on simulator to freeway drive
- Opened maps to see if the location is changing
- Waited on the server to see for location updates and was getting new results every about 3 minutes
However, I'm still not sure if this is fine enough on a real device.
I'm working on Xcode Version 6.0.1 (6A317); tested on Simulator iPhone 5s (8.0).
Adding to MagicSeth's answer, if you need to test this on a real device instead of a simulator, you can trigger a background launch with UIApplication.LaunchOptionsKey.location key by disabling and re-enabling Location Services in General > Privacy screen in the Settings app.
Depending on your scenario I would suggest two solutions:
Use a Timer or LocalNotification that periodically calls stopMonitoringSignificantLocationChanges followed by startMonitoringSignificantLocationChanges which should trigger a new location to be sent to your code (might be the same Location as before).
Build your own GPS Simulator that you start in debug builds and that will call the same delegate methods like CLLocationManager would do.
One thing I noticed in iOS 7 and Xcode 5.1.1 - If you are expecting SLC events to fire up your app into background mode, it may or may not hit the breakpoints you set. For me, sometimes the NSLog message are not even showing.
If that's the case for you, you can view NSLog outputs from the System Log. You can open the System Log from iOS Simulator's Debug menu.
In iOS 4, you can register for significant location changes. From the Apple docs: With this service, location updates are generated only when the user’s location changes significantly; thus, it is ideal for social applications or applications that provide the user with noncritical, location-relevant information. If the application is suspended when an update occurs, the system wakes it up in the background to handle the update. If the application starts this service and is then terminated, the system relaunches the application automatically when a new location becomes available. This service is available in iOS 4 and later, only on devices that contain a cellular radio.
See the Apple docs here and here.
Here is some example code to register for signification location updates:
- (void)startSignificantChangeUpdates {
// Create the location manager if it doesn't exist
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startMonitoringSignificantLocationChanges];
}
The docs say: if you leave this service running and your application is subsequently suspended or terminated, the service automatically wakes up your application when new location data arrives. At wake-up time, your application is put into the background and given a small amount of time to process the location data. Because your application is in the background, it should do minimal work and avoid any tasks (such as querying the network) that might prevent it from returning before the allocated time expires. If it does not, your application may be terminated.
Freeway drive
// MARK: - MKMapViewDelegate
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
guard let location = userLocation.location else { return }
print(location.speed)
}
Well, this is not possible as the application scope is limited to its own space and such kind of notification cannot be generated with Apple Documented APIs list.
Of course.. if any undocumented API is used, application will get rejection from apple due to the use of undocumented/private API.