Objective c 当应用程序被iPhone应用程序切换器中的用户杀死时,应用程序未更新位置(目标C)

Objective c 当应用程序被iPhone应用程序切换器中的用户杀死时,应用程序未更新位置(目标C),objective-c,location,cllocationmanager,launch,Objective C,Location,Cllocationmanager,Launch,我正在进行位置更新 当应用程序位于前台时,应用程序使用KVO更新位置 self.mapView_locUpd.myLocationEnabled = YES; [self.mapView_locUpd addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:nil]; - (void)observeValueForKeyPath:(NSString *)ke

我正在进行位置更新

当应用程序位于前台时,应用程序使用KVO更新位置

     self.mapView_locUpd.myLocationEnabled = YES;
     [self.mapView_locUpd addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:nil];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if([keyPath isEqualToString:@"myLocation"]) {
        NSLog(@"Kvo....location");
        CLLocation *location = [object myLocation];
        self.currentLatitude= [NSString stringWithFormat:@"%f",location.coordinate.latitude];
        self.currentLongitude=[NSString stringWithFormat:@"%f",location.coordinate.longitude];
        [self updateCurrentLocation];
    }
}
当应用程序处于后台(未终止)时,应用程序使用CLLocationManager更新位置

-(void)initLocationmManager
{
    if(self.locationManagerCurrent == nil)
        self.locationManagerCurrent = [[CLLocationManager alloc]init];

    self.locationManagerCurrent.delegate=self;
    self.locationManagerCurrent.desiredAccuracy= kCLLocationAccuracyBestForNavigation;  //provide precise location but more battery consumer
    self.locationManagerCurrent.activityType = CLActivityTypeOtherNavigation;
    self.locationManagerCurrent.pausesLocationUpdatesAutomatically = YES;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
        [self.locationManagerCurrent requestAlwaysAuthorization];
    }

    if ([self.locationManagerCurrent respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
        [self.locationManagerCurrent setAllowsBackgroundLocationUpdates:YES];
    }
}
-(void)configureLocationManagerOnAppinBackground{
    if(self.locationManagerCurrent == nil)
    {
        [self initLocationmManager];
    }
    [self.locationManagerCurrent startMonitoringSignificantLocationChanges];
    if ( [[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
    {
        [self.locationManagerCurrent startUpdatingLocation];
    }
}

#pragma mark -CllocationmanagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    NSLog(@"Loctaionmanager....DidUpadetLoc");
    CLLocation *location =[locations objectAtIndex:0];
    if (location.horizontalAccuracy < 0) {
        return;
    }

    self.currentLatitude= [NSString stringWithFormat:@"%f",location.coordinate.latitude];
    self.currentLongitude=[NSString stringWithFormat:@"%f",location.coordinate.longitude];
    [self updateCurrentLocation];
}
-(void)initLocationManager
{
if(self.locationManagerCurrent==nil)
self.locationManagerCurrent=[[CLLocationManager alloc]init];
self.locationManagerCurrent.delegate=self;
self.locationManagerCurrent.desiredAccuracy=KCallocationAccuracybestforNavigation;//提供精确的位置,但需要更多的电池电量
self.locationManagerCurrent.activityType=CLActivityTypeOtherNavigation;
self.locationManagerCurrent.pausesLocationUpdatesAutomatically=YES;
如果([[[UIDevice currentDevice]systemVersion]floatValue]>=8){
[self.locationmanager当前请求始终授权];
}
if([self.locationmanager当前响应选择器:@selector(setAllowBackgroundLocationUpdates:)])){
[self.locationmanager当前设置AllowsBackgroundLocationUpdate:是];
}
}
-(无效)configureLocationManagerOnAppinBackground{
if(self.locationManagerCurrent==nil)
{
[自初始化位置管理器];
}
[self.locationmanager当前开始监视重要位置更改];
if([[UIApplicationSharedApplication]应用程序状态]==UIApplicationStateBackground)
{
[self.locationManagerCurrent startUpdatingLocation];
}
}
#pragma标记-CllocationmanagerDelegate
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置
{
NSLog(@“LocationManager…DidUpadetLoc”);
CLLocation*location=[locations objectAtIndex:0];
if(位置水平精度<0){
返回;
}
self.currentLatitude=[NSString stringWithFormat:@“%f”,location.coordinate.latitude];
self.currentLength=[NSString stringWithFormat:@“%f”,location.coordinate.longitude];
[自更新当前位置];
}
使用CllocationManager,当应用程序处于后台时,位置更新非常好。假设在任何情况下,iOS暂停应用程序,然后iOS将在后台启动我的应用程序并缓慢启动位置更新

现在,如果用户从iPhone应用程序切换器中删除了应用程序,则不会出现位置更新。 所以,当应用程序被用户杀死时,我想(持续或缓慢地)更新位置。 我尝试了很多东西来启动这个应用程序。 请帮助。

您应该探索

您需要
ui背景模式
位置

此外,您还需要请求隐私-位置始终使用和提供 在Info.plist中输入“隐私-位置始终使用说明”

这将允许iOS在用户关闭应用程序时部分重新启动应用程序。iOS将向应用程序委托方法didFinishLaunchingWithOptions返回一个键UIApplicationLaunchActionsLocationKey


这个答案也应该对您有所帮助:

您是否尝试过在推送通知时更新位置?是的。我尝试在本地和远程通知(静默通知)上更新位置。但一旦应用程序被用户杀死并收到通知,用户必须通过单击通知或单击应用程序图标来启动应用程序,然后只有位置开始更新。Thanx Mike。。。!!!是的,我也处理过这个问题,我也参考了你的链接。这是我以前提到的同一个链接。任何其他调试方法。