I want to write location tracking app like "find my iphone" that runs in foreground,background and even terminating ( not running). Location will be sent to my server periodically time. I have searched in google and read many many documents,tutorials comments, codes about this topic. Then I have found this tutorial. But in this tutorial, location is sent to ".txt" file in foreground and background not terminating... I mean, when the app is killed, it is not relaunched in the background to send location ".txt" file .. So I have added and updated some codes to send location when it is not running.. However, I didn't do it... I mean, when I kill(close) the app in multitasking (double tapping home button), it is not sending to location...
Can you help me, how can I fix this problem?
Thanks in advance
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self log:[NSString stringWithFormat:@"Background location %.06f %.06f %@" , newLocation.coordinate.latitude, newLocation.coordinate.longitude, newLocation.timestamp]];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
id locationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey];
if (locationValue)
{
// create a new manager and start checking for sig changes
[self log:@"didFinishLaunchingWithOptions location key"];
m_locManager = [[CLLocationManager alloc] init];
[self log:@"didFinishLaunchingWithOptions created manager"];
m_locManager.delegate = self;
[self log:@"didFinishLaunchingWithOptions set delegate"];
[m_locManager startMonitoringSignificantLocationChanges];
[self log:@"didFinishLaunchingWithOptions monitoring sig changes"];
return YES;
}
[self log:@"didFinishLaunchingWithOptions"];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
[self log:@"applicationWillResignActive"];
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:viewController.m_significantSwitch.on forKey:@"significant"];
[userDefaults synchronize];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self log:@"applicationDidEnterBackground"];
[m_locManager startMonitoringSignificantLocationChanges];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self log:@"applicationWillEnterForeground"];
[m_locManager stopMonitoringSignificantLocationChanges];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self log:@"applicationDidBecomeActive"];
if (![window.subviews count])
{
// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
viewController.m_significantSwitch.on = [userDefaults boolForKey:@"significant"];
if (viewController.m_significantSwitch.on)
[viewController actionSignificant:nil];
}
}
- (void)applicationWillTerminate:(UIApplication *)application {
[self log:@"applicationWillTerminate"];
[m_locManager startMonitoringSignificantLocationChanges];
}
My friend , you are on wrong way. Actually there is no any way to do a task after killing of app in iOS. You can read about this here in Apple documentation.
Find My iPhone - It is an Apple App and they are using private api's to make this possible. And these type of features not available for general purpose developer programming.
So please don't go on this path.All will go in vain.
Hope this Helps you !
That is not correct, when your app is terminated it can still monitor significant location updates.
You cannot run anything when the app is killed, except you can recieve push notifications or UILocalNotifications. You could try duplicating UILocalNotifications functionality and do your stuff even when your app is killed/in bg/. BTW this is a complex thing and you could do more digging about ios7 background tasks or something.
This may help you. Getting the User’s Location
see the section Starting the Significant-Change Location Service. It says
If you leave the significant-change location service running and your iOS app is subsequently suspended or terminated, the service automatically wakes up your app when new location data arrives. At wake-up time, the app is put into the background and you are given a small amount of time (around 10 seconds) to manually restart location services and process the location data.
来源:https://stackoverflow.com/questions/18463077/ios-location-update-even-app-is-not-running-like-find-my-iphone