gpt4 book ai didi

ios - 后台定位服务在 iOS 7 中不起作用

转载 作者:IT老高 更新时间:2023-10-28 11:50:10 29 4
gpt4 key购买 nike

我最近升级了我的 iOS 设备以使用 iOS 7。我们正在开发的一个应用程序使用后台定位服务来跟踪设备位置,我们所有的测试人员都报告说该应用程序似乎不再在iOS 7 下的背景。

我们已验证该应用的后台功能已在设备的设置中启用,并且之前的版本在 iOS 6 下可以完美运行。即使设备被循环使用,该应用也会在位置更新后重新启动。

在 iOS 7 下是否还需要做其他事情才能完成这项工作?

最佳答案

这是我用来从 iOS 7 设备获取连续位置的解决方案,无论它是在前台还是后台。

您可以从博客和 github 中找到完整的解决方案和解释:-

  1. Background Location Update Programming for iOS 7 and 8

  2. Github Project: Background Location Update Programming for iOS 7 and 8

方法及说明:-

  1. 我在函数 didUpdateLocations

  2. 中每 1 分钟重新启动一次位置管理器
  3. 我允许位置管理器在关闭设备前 10 秒从设备获取位置(以节省电池)。

部分代码如下:-

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

for(int i=0;i<locations.count;i++){
CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];

if (locationAge > 30.0)
continue;

//Select only valid location and also location with good accuracy
if(newLocation!=nil&&theAccuracy>0
&&theAccuracy<2000
&&(!(theLocation.latitude==0.0&&theLocation.longitude==0.0))){
self.myLastLocation = theLocation;
self.myLastLocationAccuracy= theAccuracy;
NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
[dict setObject:[NSNumber numberWithFloat:theLocation.latitude] forKey:@"latitude"];
[dict setObject:[NSNumber numberWithFloat:theLocation.longitude] forKey:@"longitude"];
[dict setObject:[NSNumber numberWithFloat:theAccuracy] forKey:@"theAccuracy"];
//Add the vallid location with good accuracy into an array
//Every 1 minute, I will select the best location based on accuracy and send to server
[self.shareModel.myLocationArray addObject:dict];
}
}

//If the timer still valid, return it (Will not run the code below)
if (self.shareModel.timer)
return;

self.shareModel.bgTask = [BackgroundTaskManager sharedBackgroundTaskManager];
[self.shareModel.bgTask beginNewBackgroundTask];

//Restart the locationMaanger after 1 minute
self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self
selector:@selector(restartLocationUpdates)
userInfo:nil
repeats:NO];

//Will only stop the locationManager after 10 seconds, so that we can get some accurate locations
//The location manager will only operate for 10 seconds to save battery
NSTimer * delay10Seconds;
delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10 target:self
selector:@selector(stopLocationDelayBy10Seconds)
userInfo:nil
repeats:NO];
}

2014 年 5 月更新:我收到了一些请求,要求在特定时间间隔内将位置发送到服务器时添加示例代码。我已经添加了示例代码,并且还结合了 BackgroundTaskManager 的修复程序,以解决后台长时间运行的故障。如果您有任何问题,欢迎在这里加入我们的讨论:Background Location Update Programming for iOS 7 with Location Update to Server Discussion

2015 年 1 月更新:如果您想在应用程序暂停的情况下获取位置更新,请参阅:Get Location Updates for iOS App Even when Suspended

关于ios - 后台定位服务在 iOS 7 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18946881/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com