当前位置: 首页 > 知识库问答 >
问题:

后台位置服务在iOS 7中不起作用

金令秋
2023-03-14

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

我们已经验证了应用程序的背景在设备上的设置中启用,并且之前的版本在iOS6下完美运行。即使设备被循环使用,应用程序也会在位置更新后重新启动。

在iOS7下,是否还需要做些什么来使这项工作发挥作用?

共有3个答案

鲍向笛
2023-03-14

我认为他们进行了优化(可能使用运动传感器),以检测手机的“相对”静止定位,并停止位置更新。这只是一个推测,但我的测试目前显示:

  1. 开始位置更新;(测试精度为10和100米,各3次)
  2. 关闭设备屏幕,将应用程序放在后台;
  3. 将设备固定(例如放在桌子上)30分钟。

I记录的数据显示,在~15m和30s后,geo更新停止。这样,您所做的所有其他后台处理也将终止。

我的设备是iPhone 5iOS7。

我95%确定,6/6.1iOS不是这样。在哪里获得100m精度的地理更新,可以让你在后台连续运行。

使现代化

如果每8分钟重新启动位置管理器,它应该连续运行。

更新#2

我最近还没有测试过这个,但这是我写这篇文章时重启它的方式。我希望这是有帮助的。

- (void)tryRestartLocationManager
{
    NSTimeInterval now = [[NSDate date] timeIntervalSince1970];

    int seconds = round(floor(now - locationManagerStartTimestamp));

    if ( seconds > (60 * 8) ) {
        [locationManager stopUpdatingLocation];
        [locationManager startUpdatingLocation];
        locationManagerStartTimestamp = now;
    }
}
令狐泓
2023-03-14

如果你看看WWDC 2013年会议视频#204-多任务pdf的新功能,第15页清楚地提到,如果用户从应用切换器杀死它,应用程序不会在后台启动。请看图片,

闻安宜
2023-03-14

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

您可以在博客和github上找到完整的解决方案和说明:-

>

  • iOS7和8的后台位置更新编程

    Github项目:iOS 7和iOS 8的后台位置更新编程

    方法及说明:-

    >

  • 我在函数didUpdateLocations中每隔1分钟重启一次位置管理器

    我允许位置管理器在关闭设备之前从设备获取位置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的修复程序,以解决后台在较长时间内运行时出现的问题。如果您有任何问题,欢迎您加入我们的讨论:iOS 7后台位置更新编程与服务器位置更新讨论

    2015年1月更新:如果您想在应用程序暂停时获得位置更新,请参阅:在iOS应用程序暂停时获得位置更新

  •  类似资料:
    • 我的背景粘性服务是杀死奥利奥和更高的设备,任何解决方案,以获得位置的背景服务时,活动是在后台

    • 问题内容: 我只是用iOS 11 SDK重建了我的应用程序,试图删除现在总是出现的应用程序。我以为“很棒,那很成功”,只是发现定位服务现在根本无法正常工作。 该应用程序过去曾与iOS 10配合使用-有人听到了吗? 问题答案: 看来苹果已经添加了另一个隐私功能。用户现在可以覆盖我们并将其降级为-这意味着作为开发人员,我们现在必须在 我发现他们已经添加了新密钥 但是,使用此新密钥后,位置服务仍然无法正

    • 注意此问题仅在android上 > 我在后台实现了位置服务,以在应用程序在后台时获取位置更新(应用程序终止时我不需要位置,只在应用程序在后台时需要) 我实现了一个监听器,在用户启动/终止应用程序时更新实时数据库(状态) 当我终止应用程序时,“onDisconnect”不启动(仅在android上),即使过了很长时间。当我删除了后台定位功能,一切都完美地工作。 我最初的怀疑是,当后台定位服务运行时,

    • 我正在构建一个在后台工作的iOS应用程序,并每3分钟将用户的位置发布到服务器上(因为这是iOS 7上的最长后台执行时间)。但是,有一个问题,后台服务在随机时间终止。所以有时候它可以在后台运行2个小时,有时候7个小时,然后3个小时,然后是随机的,依此类推。 下面的代码产生错误。我已经能够检测到它何时终止,也就是[UIApplication sharedApplication]何时终止。剩余的背景时间

    • 问题内容: 哪种方法更好,直接像这样执行 或通常在类内部声明? 问题答案: 在第二段代码中,必须在调用接口的方法之前先调用属性。 在第一段代码中,您可以直接访问接口方法。 因此,如果您知道每个方法调用都会花费cpu时间,则直接在类中实现它而不是将其作为属性将是有益的。 在这种情况下,您有1个引用,可以使用该引用访问LocationListener的方法 在这种情况下,您有2个引用,一个是 Back

    • 我有一个后台服务,每12分钟更新一次位置。我们希望每5-6分钟测试一次位置更新,但在Android文档中我们发现: 为了降低功耗,Android8.0(API Level26)限制了后台应用程序检索用户当前位置的频率。应用程序每小时只能接收几次位置更新。 限制是每小时固定次数?