获取 iOS 应用程序在后台甚至被杀死时的位置
Getting location for an iOS app when it is in the background and even killed
我的应用程序需要在应用程序处于活动状态以及处于非活动状态和被终止时获取用户的位置。当用户的位置靠近商店时,应用程序必须发送本地通知。
我不确定到底发生了什么,但我无法让我的应用程序在后台获取位置并在被杀死时将其唤醒。
我有一个位置管理器(单例,用于 whenInUse 和 Always 两种情况),并且我在 .plist
中定义了 NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription
我正在做的是:
- (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 阅读的内容就足够了。
要在后台获取位置,请使用以下代码。每次重启后台任务都会让你的应用运行长时间处于后台。
要使用它,您需要在项目设置的 Capabilities 中打开 Background Mode Background Fetch 和 位置更新 已开启。
- (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
}];
}
}
}
我的应用程序需要在应用程序处于活动状态以及处于非活动状态和被终止时获取用户的位置。当用户的位置靠近商店时,应用程序必须发送本地通知。
我不确定到底发生了什么,但我无法让我的应用程序在后台获取位置并在被杀死时将其唤醒。
我有一个位置管理器(单例,用于 whenInUse 和 Always 两种情况),并且我在 .plist
中定义了 NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription我正在做的是:
- (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 阅读的内容就足够了。
要在后台获取位置,请使用以下代码。每次重启后台任务都会让你的应用运行长时间处于后台。
要使用它,您需要在项目设置的 Capabilities 中打开 Background Mode Background Fetch 和 位置更新 已开启。
- (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
}];
}
}
}