简体   繁体   English

应用终止时后台位置跟踪不起作用

[英]Background location tracking not working when the app is terminated

I have an app which is set to monitor entry into regions. 我有一个应用程序,用于监视进入各个地区的情况。 On entry, the app will alert the user with a local notification. 进入时,该应用程序将通过本地通知提醒用户。

This works fine when the app is on, or in the background. 当应用处于开启状态或在后台运行时,此方法工作正常。 However if the app is terminated, the region monitoring never raises the local notification. 但是,如果应用程序终止,则区域监视将永远不会引发本地通知。

I have set my "background modes" key in the info.plist. 已经在info.plist中设置了“背景模式”键。

Could this be because my CLLocation code is not in the AppDelegate (instead it is in a singleton)? 可能是因为我的CLLocation代码不在AppDelegate中(而是在单例中)吗?

Could this be because it's not possible to run code to raise the location notification from a terminated state? 难道是因为无法运行代码以从终止状态引发位置通知吗?

Here's my code when the region is entered: 输入区域后,这是我的代码:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {

    UIApplication* app = [UIApplication sharedApplication];
    UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];

    notifyAlarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];;
    notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
    notifyAlarm.repeatInterval =NSDayCalendarUnit;
    notifyAlarm.alertBody = [Installation currentInstallation].reminderText;

    [app scheduleLocalNotification:notifyAlarm];
}

There are 2 things happen: a) If the application is suspended when an update occurs, the system wakes it up in the background to handle the update. 发生两件事:a)如果应用程序在更新发生时被挂起,系统会在后台将其唤醒以处理更新。

b) If the application starts this service and is then terminated, the system relaunches the application automatically when a new location becomes available. b)如果应用程序启动了该服务然后又终止了,那么当有新位置可用时,系统会自动重新启动应用程序。

What we can now do is turn on significant location updates when the user hits the home key and we can let the system wake us up when needed. 现在,我们可以做的就是在用户按下Home键时打开重要的位置更新,我们可以让系统在需要时唤醒我们。

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

    // You will also want to check if the user would like background location
    // tracking and check that you are on a device that supports this feature.
    // Also you will want to see if location services are enabled at all.
    // All this code is stripped back to the bare bones to show the structure
    // of what is needed.

    [locationManager startMonitoringSignificantLocationChanges];
}

Then to perhaps switch to higher accuracy when the application is started up, use; 然后在应用程序启动时使用,也许切换到更高的精度;

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

       [locationManager stopMonitoringSignificantLocationChanges];
       [locationManager startUpdatingLocation];
}

Next you'll likely want to change your location manager delegate to handle background location updates. 接下来,您可能需要更改位置管理器委托以处理后台位置更新。

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

    BOOL isInBackground = NO;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        isInBackground = YES;
    }

    // Handle location updates as normal.

    if (isInBackground)
    {
        // Do, if you have to send location to server, or what you need
    }
    else
    {
        // ...
    }
}

Please Note: Location monitoring in background will take effect on battery usage. 请注意:后台位置监视将对电池使用量产生影响。

This code is taken from http://www.mindsizzlers.com/2011/07/ios-background-location/ 该代码取自http://www.mindsizzlers.com/2011/07/ios-background-location/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在运行并在后台跟踪我的位置时,如何避免我的应用终止? - How to avoid my app to be terminated, when it's running and tracking my location in the background? 应用程序终止/暂停时的后台用户位置 - Background user location when app is terminated/suspended 应用程序终止时使用位置更新 - Working with location updates when app is terminated 在应用终止时跟踪用户位置 - Tracking user location while app is terminated 应用终止后,位置背景模式能否快速运行? - Does the location background mode work in swift when the app is terminated? 当应用程序处于前台、后台或终止时获取设备位置 state in IOS - Getting device location when the app is in foreground, background or terminated state in IOS 应用终止时更新位置 - Update Location when app is terminated 当应用程序在后台时,位置跟踪会在一段时间后停止 - Location tracking stops after a while when app is in the background 当应用程序不在后台运行时,使用startmonitoringsignficantlocation更改跟踪设备位置 - tracking device location using startmonitoringsignficantlocationchanges when app not running in background 当应用程序在后台或终止时,FCM通知iOS无法正常工作 - FCM Notification iOS not working when app is in background or terminated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM