在iOS应用程序处于后台甚至死亡时获取位置信息

我的应用程序需要获取用户的位置,当应用程序处于活动状态,当它是无效的和死亡。 当用户的位置靠近商店时,应用程序必须发送本地通知。

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

我有一个位置pipe理器(singleton,用于两种情况whenInUse和Always),我有NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription定义在.plist

我正在做的是:

- (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]; } 

我做错了什么? 我不确定是否严格需要使用geofencing,但是用startMonitoringSignificantLocationChanges读取的东西就足够了。

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

要使用此function,需要打开背景获取位置更新打开项目设置中的function中的背景模式

 - (void)applicationDidEnterBackground:(UIApplication *)application { if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking IE 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 }]; } } }