Note that there are some explanatory texts on larger screens.

plurals
  1. POtracking device location using startmonitoringsignficantlocationchanges when app not running in background
    primarykey
    data
    text
    <p>I want to track location of iOS device even if the app is not running in background. My main purpose in that I want to fetch device location and send it to server using web service call. For this purpose I followed this tutorial <a href="http://www.mindsizzlers.com/2011/07/ios-background-location/" rel="nofollow">http://www.mindsizzlers.com/2011/07/ios-background-location/</a> but I am not able to achieve the same.</p> <p>For now instead of sending coordinates to server I am trying to log the location in a text file "<strong>but I am not able to achieve it when my app is not running in background</strong>"and I have been using following code for the same. </p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ if (!locationManager) { locationManager = [[CLLocationManager alloc]init]; } [locationManager setDelegate:self]; // // [locationManager startMonitoringSignificantLocationChanges]; // if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey] ) { [locationManager startMonitoringSignificantLocationChanges]; } else{ [locationManager startUpdatingLocation]; } } </code></pre> <p>-</p> <pre><code>- (void)applicationWillTerminate:(UIApplication *)application { [locationManager startMonitoringSignificantLocationChanges]; } </code></pre> <p>-</p> <pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application { [locationManager startMonitoringSignificantLocationChanges]; } </code></pre> <p>-</p> <pre><code>- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ BOOL isInBackground = NO; if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { isInBackground = YES; NSLog(@"is in background"); } // Handle location updates as normal, code omitted for brevity. // The omitted code should determine whether to reject the location update for being too // old, too close to the previous one, too inaccurate and so forth according to your own // application design. if (isInBackground) { [self sendBackgroundLocationToServer:[locations lastObject]]; } else { // ... } } </code></pre> <p>-</p> <pre><code>- (void) log:(NSString*)msg { NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setTimeStyle:NSDateFormatterMediumStyle]; NSString * logMessage = [NSString stringWithFormat:@"%@ %@", [formatter stringFromDate:[NSDate date]], msg]; NSString * fileName = [self locationPath]; FILE * f = fopen([fileName cString], "at"); fprintf(f, "%s\n", [logMessage cString]); fclose (f); } </code></pre> <p>-</p> <pre><code>- (NSString*)locationPath { NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; return [path stringByAppendingPathComponent:@"locationPath.txt"]; } </code></pre> <p>-</p> <pre><code>-(void) sendBackgroundLocationToServer:(CLLocation *)location { // REMEMBER. We are running in the background if this is being executed. // We can't assume normal network access. // bgTask is defined as an instance variable of type UIBackgroundTaskIdentifier // Note that the expiration handler block simply ends the task. It is important that we always // end tasks that we have started. bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ [[UIApplication sharedApplication] endBackgroundTask:bgTask]; }]; [self log:[NSString stringWithFormat:@"Background location %.06f %.06f %@" , location.coordinate.latitude, location.coordinate.longitude, location.timestamp]]; // ANY CODE WE PUT HERE IS OUR BACKGROUND TASK // For example, I can do a series of SYNCHRONOUS network methods (we're in the background, there is // no UI to block so synchronous is the correct approach here). // ... // AFTER ALL THE UPDATES, close the task if (bgTask != UIBackgroundTaskInvalid) { [[UIApplication sharedApplication] endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; } } </code></pre> <p>-</p> <pre><code>- (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } </code></pre> <p>-</p> <pre><code>- (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. // [locationManager stopMonitoringSignificantLocationChanges]; [locationManager startUpdatingLocation]; } </code></pre> <p>Any help would be much appreciated. </p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload