Vlad Bogdan
Vlad Bogdan

Reputation: 701

How to keep GPS app running in background without pauses

I have a problem keeping the GPS running in background.

I succeded to keep the GPS running when the app is in background, but it stops sometimes. I noticed that it stops when i enter in a building and i stay there for 2-3 hours. "Stops" means that i don't receive any GPS point even though the location service icon is still on. Then when i get out i don't receive any points until I bring my application to foreground.

I set the pausesLocationUpdatesAutomatically property to NO.

So, everything works fine until i get into a location with weak gps signal. When i get out of that area i don't receive points anymore, even though I should because the gps signal is now good.

And here is my code when the applications enters in background:

- (void)applicationWillResignActive:(UIApplication *)application
{
    NSLog(@"[AppDelegate]: Application entering in background!");
    if(doesUserWantGPSTracking)
    {
    [self stopGPS];

    UIApplication *app = [UIApplication sharedApplication];

    bgTask = [app beginBackgroundTaskWithExpirationHandler:
              ^{

                    dispatch_async(dispatch_get_main_queue(),
                    ^{

                        if( bgTask != UIBackgroundTaskInvalid )
                        {
                            [app endBackgroundTask:bgTask];
                            bgTask = UIBackgroundTaskInvalid;
                        }

                    });

              }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
        ^{

            [self startGPS];

            dispatch_async(dispatch_get_main_queue(), ^{

                if( bgTask != UIBackgroundTaskInvalid )
                {
                    [app endBackgroundTask:bgTask];
                    bgTask = UIBackgroundTaskInvalid;
                }

            });

        });
    }
    }

Upvotes: 0

Views: 2888

Answers (3)

Capan
Capan

Reputation: 756

If you are using a phone instead of GPS you can use Network for location services. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 300, 5, konumDinleyicisiNetwork);

Upvotes: 0

Horst
Horst

Reputation: 1739

In your info plist, add Required background modes

and set one item to App registers for location updates

Updated:

As ios return to - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation when there is a significant distance change. I guess you need to change:

// Set the movement threshold.
locationManager.distanceFilter = 500; // meters

Upvotes: 3

AlexWien
AlexWien

Reputation: 28767

inside a building you will often not get any GPS location. GPS needs free view to sky. If you dont get a position, of course the only reason to keep your app running in background is missing, therefore ios puts your App inactive till you get your next GPS location (near the window,or outside)

Further you stop gps when you. in background, [self stopGPS] why? let it alive!

Upvotes: 0

Related Questions