简体   繁体   中英

iOS 7 - Background Location app getting killed when getting a background location

I have checked previous stackoverflow regarding this issue and have not gotten a proper answer. I have CLLocationManager setup with region monitoring and getting location updates in the background. I have enable the capabilities of the app to be Backgroud location update.. However I see that when I get the location update I get killed when I do some processing.. ie get the location and send it over to a server.. Any ideas why this is happening.. I have added log messages for all app delegates and I don't see any of them being fired either.. Any pointers will be appreciated..

For #1 below are you suggesting:

- (void) _processNewLocationFromManager:manager

{
    // Process
}
- (void)locationManagerUpdatedLocation:(CSLocationManager *)manager
{
    BOOL isBackground = [[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground;
    if (!isBackground || (isBackground && self.bgTask == UIBackgroundTaskInvalid)) {
        if (isBackground ) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                [self beginBackgroundUpdateTask];
                [self _processNewLocationFromManager:manager];
                [self endBackgroundUpdateTask];
            });
        } else {
            [self _processNewLocationFromManager:manager];
        }
    }
}

- (void) beginBackgroundUpdateTask
{
    self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundUpdateTask];
    }];
}

- (void) endBackgroundUpdateTask
{
    [[UIApplication sharedApplication] endBackgroundTask: self.bgTask];
    self.bgTask = UIBackgroundTaskInvalid;
}

1) Its supposed to redundant and not needed when you use background location, however I've found if you're not getting the execution behaviour you expect when using location then throwing in a beginBackgroundTaskWithExpirationHandler when updateLocations: gets called can sometimes fix issues like this.

2) Another thing that you could try is setting pausesLocationUpdatesAutomatically to NO to see if it fixes things for you, but if it does it could use the batteries some more.

3) Also another thing to try is if you have other background modes set at the same time as location (in particular backgroundFetch and remoteNotifications) you could try toggling them off temporarily to see if its having an adverse affect.

4) One final thing, see if there are any other location based apps installed on your phone which are currently running, in my experience I've found if there's more than one location app running and they have different location manager setting set then one app can affect the behaviour of the other.

None of these should have any affect on your program and they might not. And people reading this might think these suggestions are crazy and just should not work.However I've been developing a couple of different location apps in parallel for the past few months and after many many many many debug sessions, logging sessions, observing location app behaviour when running in the background, I've come to the definite conclusion and am 100% convinced that all is not as it seems and is supposed to be (nor as is documented, nor is as it should be according to accumulated group wisdom and stack overflow answers) when it comes to location behaviour.

There's something mysterious and screwy going on in location land sometimes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM