获取iOS应用程序在后台甚至被杀死时的位置

获取iOS应用程序在后台甚至被杀死时的位置,ios,objective-c,core-location,appdelegate,Ios,Objective C,Core Location,Appdelegate,我的应用程序需要在应用程序处于活动状态以及处于非活动状态和被终止状态时获取用户的位置。当用户的位置靠近商店时,应用程序必须发送本地通知 我不确定到底发生了什么,但我无法让我的应用程序在后台获取位置,并在被杀死时将其唤醒 我有一个位置管理器(singleton,用于不使用和始终使用这两种情况),并且在.plist中定义了NSLocationAlwaysUsageDescription和NSLocationWhenUsageDescription 我正在做的是: - (BOOL)applicatio

我的应用程序需要在应用程序处于活动状态以及处于非活动状态和被终止状态时获取用户的位置。当用户的位置靠近商店时,应用程序必须发送本地通知

我不确定到底发生了什么,但我无法让我的应用程序在后台获取位置,并在被杀死时将其唤醒

我有一个位置管理器(singleton,用于不使用和始终使用这两种情况),并且在.plist中定义了NSLocationAlwaysUsageDescription和NSLocationWhenUsageDescription

我正在做的是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //The app has been killed/terminated (not in background) by iOS or the user.
    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]){

        _locationManager = [CoreLocationManager sharedInstance];
        _locationManager.isAppActive = NO;
        _locationManager.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        _locationManager.locationManager.activityType = CLActivityTypeOtherNavigation;

        if ([_locationManager.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [_locationManager.locationManager requestAlwaysAuthorization];
        }

        [_locationManager addLocationManagerDelegate:self];
    }
}


- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if (_locationManager.locationManager){
        _locationManager.isAppActive = YES;
        [_locationManager.locationManager stopMonitoringSignificantLocationChanges];
    }

    _locationManager = [CoreLocationManager sharedInstance];

    if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [_locationManager.locationManager requestAlwaysAuthorization];
    }

    if ([_locationManager.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [_locationManager.locationManager requestWhenInUseAuthorization];
    }

    [_locationManager addLocationManagerDelegate:self];

    [_locationManager.locationManager startUpdatingLocation];

}


- (void)applicationDidEnterBackground:(UIApplication *)application
{
    _locationManager.isAppActive = NO;

    if (_locationManager.locationManager){
        [_locationManager.locationManager stopUpdatingLocation];
        [_locationManager.locationManager stopMonitoringSignificantLocationChanges];
    }

    if ([_locationManager.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [_locationManager.locationManager requestAlwaysAuthorization];
    }

    [_locationManager.locationManager startMonitoringSignificantLocationChanges];

}

我做错什么了吗?我不确定是否必须严格使用地理围栏,但对于我在startMonitoringSignificantLocationChanges中读到的内容,就足够了。

要在后台获取位置,请使用以下代码。它会让你的应用程序在后台运行很长时间,每次都会重新启动后台任务

要使用此功能,您需要在项目设置中启用后台获取和位置更新的功能中的后台模式

- (void)applicationDidEnterBackground:(UIApplication *)application {

    if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4

        if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
            UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance

            __block UIBackgroundTaskIdentifier background_task; //Create a task object

            background_task = [application beginBackgroundTaskWithExpirationHandler: ^{
                [application endBackgroundTask:background_task]; //Tell the system that we are done with the tasks
                background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
                //System will be shutting down the app at any point in time now
            }];
        }
    }
}

谢谢Utsav!!我必须在哪里插入ApplicationIdentinterBackground的locationmanager代码?@Stacky不需要您在didEnterBackground中的代码。您永远不需要调用StopUpdateLocation,因为您需要连续的位置更新。如果调用stop UpdateLocation,用户将停止获取位置更新。只有当用户从应用程序注销时,您才应该这样做对不起,如果我问了一个愚蠢的问题@utsav,那么是否不需要调用StopUpdateLocation(因为在应用程序运行时使用)然后调用startMonitoringSignificantLocationChanges?@Stacky startUpdatingLocation和startMonitoringSignificantLocationChanges不能同时使用。这是或,因为它们都将标题和位置更改传递给同一个委托方法。locationManager:didUpdateToLocation:FromLocation这真的有效吗?“每次都重新启动后台任务”这是怎么发生的?