Ios 应用程序终止时后台位置跟踪不工作
我有一个应用程序,它被设置为监视进入区域的情况。进入时,应用程序将向用户发出本地通知 当应用程序处于打开状态或在后台时,此功能可以正常工作。但是,如果应用程序被终止,区域监视将不会引发本地通知 我已在info.plist中设置了我的“背景模式”键 这可能是因为我的CLLocation代码不在AppDelegate中(而是在单例中) 这可能是因为无法运行代码从终止状态引发位置通知吗 输入地区时,我的代码如下:Ios 应用程序终止时后台位置跟踪不工作,ios,objective-c,background,location,cllocation,Ios,Objective C,Background,Location,Cllocation,我有一个应用程序,它被设置为监视进入区域的情况。进入时,应用程序将向用户发出本地通知 当应用程序处于打开状态或在后台时,此功能可以正常工作。但是,如果应用程序被终止,区域监视将不会引发本地通知 我已在info.plist中设置了我的“背景模式”键 这可能是因为我的CLLocation代码不在AppDelegate中(而是在单例中) 这可能是因为无法运行代码从终止状态引发位置通知吗 输入地区时,我的代码如下: - (void)locationManager:(CLLocationManager *
- (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];
}
发生了两件事:
a) 如果更新发生时应用程序被挂起,系统会在后台将其唤醒以处理更新
b) 如果应用程序启动此服务,然后终止,则系统会在新位置可用时自动重新启动应用程序
我们现在可以做的是在用户点击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];
}
然后,为了在应用程序启动时切换到更高的精度,请使用
-(void) applicationDidBecomeActive:(UIApplication *) application
{
[locationManager stopMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
}
接下来,您可能需要更改location manager代理以处理后台位置更新
-(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
{
// ...
}
}
请注意:后台位置监控将对电池使用产生影响
这段代码取自,你真应该相信Roger,他在iOS 10上试过写了这段代码。在被iOS终止或用户强制杀死应用程序后,它不会唤醒应用程序。请检查一下?