3

I'm developing an app which sends notifications when you are nearby of promoted places. My problem is when I go to background and then I quit the app, I don't want the location services working when the app doesn't work (but I want them to work in background).

I saw only 3 apps which close the gps when the app is closed and I want to know how they did that, Facebook, Google Maps and Apple Maps, not Foursquare, not FieldTrips...

Thank you everybody.

Javier Peigneux
  • 567
  • 4
  • 14
  • as per my understanding you want to remove location when app quit but run location when it is in background. for that in `applicationWillTerminate` method in `AppDelegate`. you need to add `stopUpdatingLocation` or `stopMonitoringSignificantLocationChanges` and make location object to `nil`. Maybe it will help you. – ChintaN -Maddy- Ramani Jul 16 '14 at 10:34
  • I'm sorry, it's not that easy... when you close the app and you are in background it didn't trigger applicationWillTerminate... – Javier Peigneux Jul 16 '14 at 10:46
  • Oh now i can understand your question . :) – ChintaN -Maddy- Ramani Jul 16 '14 at 10:49

3 Answers3

1

you can add an observer for UIApplicationWillTerminateNotification where you start locationManager and than stop location updates

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationWillTerminate:)
                                                 name:UIApplicationWillTerminateNotification
                                               object:nil];

method to perform when you receive the notification

- (void)applicationWillTerminate:(NSNotification *)notification {
//stop location updates
}
Guy S
  • 1,424
  • 1
  • 15
  • 34
  • I tried but unfortunately it's not that easy... when you close the app and you are in background it didn't send that notification. – Javier Peigneux Jul 16 '14 at 10:47
  • @JavierPeigneux What is the way you tested that? I see `-(void)applicationWillTerminate:(UIApplication *)application` and `UIApplicationWillTerminateNotification` are called when other app is in foreground and we close our app by swiping up the snapshot. – Alex Peda Jul 16 '14 at 10:55
  • @AlexPeda The problem is when the app is in background and then you close it by swiping up the snapshot... I tested it with logs, breakpoints and I have the code to stop the gps that is working in the rest of the code and it never works there... – Javier Peigneux Jul 16 '14 at 11:03
  • I have posted the new answer to the topic. Hope, it will be helpful. Good luck :) – Alex Peda Jul 16 '14 at 11:12
  • check out this post: http://stackoverflow.com/questions/9077105/grey-gps-arrow-shown-in-statusbar-although-location-based-application-killed-usi, the second point in the OP questions is the problem you are describing? if so, the accepted answer should solve it – Guy S Jul 16 '14 at 11:30
1

I found the correct answer to my question becouse of @GuyS second post:

Adding that in your AppDelegate.m applicationDidEnterBackground

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication *app = [UIApplication sharedApplication];
    if ([app respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)]) {
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
            // Synchronize the cleanup call on the main thread in case
            // the task actually finishes at around the same time.
            dispatch_async(dispatch_get_main_queue(), ^{
                if (bgTask != UIBackgroundTaskInvalid)
                {
                    [app endBackgroundTask:bgTask];
                    bgTask = UIBackgroundTaskInvalid;
                }
            });
        }];
    } 
}

And declaring that variable:

UIBackgroundTaskIdentifier bgTask;

After that you only have to stop your location services in applicationWillTerminate...

Thank you for your replies.

Javier Peigneux
  • 567
  • 4
  • 14
  • This should be an accepted answer, it addresses the issue of calling applicationWillTerminate when the app is in the background. – Amro Younes Dec 14 '14 at 19:02
0

The solution provided by @GuyS in this topic should work. I'm getting the UIApplicationWillTerminateNotification in case the app is in background and then I close it by swiping up the snapshot. Please check whether you work correctly with NSNotificationCenter (especially adding and removing notification). Plus, please check the object you subscribed on the notification is alive when the app is in background.

Another similar solution is to place the code that disables GPS in appropriate UIApplicationDelegate callback in your AppDelegate method.

- (void)applicationWillTerminate:(UIApplication *)application {
//stop location updates
}
Alex Peda
  • 4,210
  • 3
  • 22
  • 31