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

时间:2017-04-26 13:52:50

标签: 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 *)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];
}

使用CllocationManager,当应用程序在后台时,位置会更新。假设在任何情况下,iOS暂停应用程序,然后iOS将在后台启动我的应用程序并慢慢启动位置更新。

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

1 个答案:

答案 0 :(得分:0)

您应该探索https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

您需要使用UIBackgroundModes密钥location

您还需要请求隐私 - 位置始终使用并提供 &#34;隐私 - 位置始终使用说明&#34;密钥在您的Info.plist

这将允许iOS在用户杀死应用时部分重新启动您的应用。 iOS将向应用委托方法didFinishLaunchingWithOptions返回密钥UIApplicationLaunchOptionsLocationKey。

此答案也可以帮助您:How to Get Location Updates for iOS 7 and 8 Even when the App is Suspended