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

编程入门 行业动态 更新时间:2024-10-23 07:38:45
本文介绍了在iOS应用在后台甚至被杀死时获取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当应用处于活动状态,处于非活动状态并被杀死时,我的应用需要获取用户的位置.当用户的位置靠近商店时,应用程序必须发送本地通知.

My app needs to get the user's location when app is active and when it's inactive and killed. When the user's location is near to a store the app has to send a local notification.

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

I'm not sure what exactly is happening, but I'm not able to make my app get the location in the background and wakes it up when is killed.

我有一个位置管理器(单个,分别用于whenInUse和Always两种情况),并且在.plist中定义了NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription.

I have a location manager (singleton, used for boths cases whenInUse and Always), and I have both NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription defined in .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]; }

我做错什么了吗?我不确定是否一定要使用地理围栏,但是对于我用startMonitoringSignificantLocationChanges读取的内容来说,就足够了.

Do I make something wrong? I'm not sure if it's strictly necessary to use geofencing, but for the things I've read with startMonitoringSignificantLocationChanges is enough.

推荐答案

要在后台获取位置,请使用以下代码.每次重新启动后台任务,都会使您的应用在后台长时间运行.

To get a location in the background, use the following code. It will make your app run in the background for a long time by restarting the background task everytime.

要使用此功能,您需要使用 Background Fetch 和 Location Updates 在项目设置的 Capabilities 中打开 Background Mode . /strong>已打开.

To use this, you need to turn on Background Mode in Capabilities in project settings with Background Fetch and Location Updates turned on.

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

更多推荐

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

本文发布于:2023-11-27 08:31:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用在   被杀   死时   后台   位置

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!