[英]After 15 mins Background Location update is not working in ios?
我已经根据计时器(像每1分钟)在ios中实现了后台位置更新。 它的工作良好,并获得位置(纬度和经度值),但15点后断开连接。 我无法再触发它。 谁能帮帮我。
这是我的代码。
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[GlobalClass sharedGlobals].locationManager stopMonitoringSignificantLocationChanges];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
[[GlobalClass sharedGlobals].locationManager requestAlwaysAuthorization];
}
[[GlobalClass sharedGlobals].locationManager startMonitoringSignificantLocationChanges];
int timeInterval = 60; // (i.e., 1 mins )
if (!setTimer) {
setTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target: self
selector: @selector(toMakeServerCall) userInfo: nil repeats: YES];
}
}
#pragma mark - Send Current lat & long to the server
- (void)toMakeServerCall {
NSString *latitude = [NSString stringWithFormat:@"%f",[[GlobalClass sharedGlobals] currentLocationObject].coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f",[[GlobalClass sharedGlobals] currentLocationObject].coordinate.longitude];
NSLog(@"Current Lat is :%@ \n Current Long is :%@ ",latitude,longitude);
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http:www.google.com/myLatitude is: %@ and My Longitude is: %@",latitude,longitude]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
}
提前致谢。
当您使用“重大更改”服务(或坦率地说,是任何位置服务)时,您无需使用计时器。 位置发生变化时,操作系统会打电话给您 ,而不是相反。
关于您的计时器,当应用暂停时,计时器将不再运行。 因此,请勿使用计时器。 但是,借助重大更改服务,当设备检测到它已经移动了很远的距离时,您的应用将被唤醒并得到通知。 如《 位置和地图编程指南:启动重要更改位置服务 》所述:
如果您使重大更改位置服务保持运行状态,并且随后您的iOS应用被暂停或终止,则当新位置数据到达时,该服务会自动唤醒您的应用。 唤醒时,该应用程序将进入后台,并且您会得到一小段时间(大约10秒)来手动重新启动位置服务并处理位置数据。 (您必须先在后台手动重新启动位置服务,然后才能交付任何未决的位置更新,如了解何时启动位置服务中所述 。)由于您的应用程序在后台运行,因此它必须做最少的工作并且避免执行任何任务(例如查询网络),可能会阻止它在分配的时间到期之前返回。 否则,您的应用将被终止。 如果iOS应用需要更多时间来处理位置数据,则可以使用
UIApplication
类的beginBackgroundTaskWithName:expirationHandler:
方法请求更多后台执行时间。
即使您将pausesLocationUpdatesAutomatically
自动设置为false,也可能会暂停位置更新。 好消息是,您可以在暂停更新时收到通知,因此可以重新启动位置更新。
像这样在您的CLLocationManagerDelegate
定义locationManagerDidPauseLocationUpdates
:
func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager) {
manager.startUpdatingLocation()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.