当应用程序被杀时,位置不会在行走时更新

时间:2016-10-27 06:58:24

标签: ios objective-c core-location cllocationmanager appdelegate

我有一个功能,当app被杀或终止时,我应该获得位置的纬度和经度。

当我骑车时,我会在应用程序被杀/终止时获得经纬度。

但是当我开始走几分钟然后我没有得到应用程序被杀/终止的经度和纬度。

以下是我的逻辑。

+ (CLLocationManager *)sharedLocationManager {
static CLLocationManager *_locationManager;

@synchronized(self) {
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        _locationManager.allowsBackgroundLocationUpdates = YES;
        _locationManager.pausesLocationUpdatesAutomatically = NO;
        _locationManager.activityType = CLActivityTypeOther;
        _locationManager.distanceFilter = kCLDistanceFilterNone;
    }
}
      return _locationManager; 
}

 - (id)init {
    if (self==[super init]) {
    //Get the share model and also initialize myLocationArray
    self.shareModel = [LocationShareModel sharedModel];
    self.shareModel.myLocationArray = [[NSMutableArray alloc]init];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
    return self;
}

-(void)applicationEnterBackground{
CLLocationManager *locationManager = [LocationTracker sharedLocationManager];
locationManager.delegate = self;
[locationManager stopMonitoringSignificantLocationChanges];

if(IS_OS_8_OR_LATER) {
    [locationManager requestAlwaysAuthorization];
}
[locationManager startMonitoringSignificantLocationChanges];

//Use the BackgroundTaskManager to manage all the background Task
self.shareModel.bgTask = [BackgroundTaskManager sharedBackgroundTaskManager];
[self.shareModel.bgTask beginNewBackgroundTask];
}

上面是我从appdelegate调用的Locationmanager类

在AppDelegate.m文件中,我写的如下

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

 UIAlertView * alert;

//We have to make sure that the Background App Refresh is enable for the Location updates to work in the background.
if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){

    alert = [[UIAlertView alloc]initWithTitle:@""
                                      message:@"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh"
                                     delegate:nil
                            cancelButtonTitle:@"Ok"
                            otherButtonTitles:nil, nil];
    [alert show];

}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){

    alert = [[UIAlertView alloc]initWithTitle:@""
                                      message:@"The functions of this app are limited because the Background App Refresh is disable."
                                     delegate:nil
                            cancelButtonTitle:@"Ok"
                            otherButtonTitles:nil, nil];
    [alert show];

} else{
    self.locationTracker = [[LocationTracker alloc]init];
    [self.locationTracker startLocationTracking];

        //Send the best location to server every 60 seconds
        //You may adjust the time interval depends on the need of your app.
    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {

        NSTimeInterval time = 10;
        self.locationUpdateTimer =
        [NSTimer scheduledTimerWithTimeInterval:time
                                         target:self
                                       selector:@selector(updateLocation)
                                       userInfo:nil
                                        repeats:YES];
    }


}

return YES;
}

任何提示?感谢...

1 个答案:

答案 0 :(得分:0)

您要求"重要位置变更。"我对API和相关区域监测的研究和经验是,由于它使用蜂窝塔代替GPS,因此分辨率非常粗糙。我们尝试在30米处使用它并且它不稳定。我们把它撞到了100米,它更好。似乎大多数人建议200米用于区域监测,因此步行不太可能引发“重要位置变化”并不令人惊讶。一段时间以来,只需几分钟就可以走200米。