后台位置服务在iOS 7中不起作用 [英] Background Location Services not working in iOS 7

查看:112
本文介绍了后台位置服务在iOS 7中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've recently upgraded my iOS devices to use iOS 7. One of the apps that we're developing uses background location services to track device location and all of our testers have reported that the app no longer appears to track in the background under iOS 7.

我们已经确认在设备上的设置中启用了应用程序的后台处理,并且之前的版本在iOS 6下运行良好。即使设备也是如此循环,应用程序将在位置更新后重新启动。

We have verified that backgrounding for the app is enabled in the settings on the device and the previous build worked flawlessly under iOS 6. Even if the device were cycled, the app would restart after a location update.

在iOS 7下还有什么需要做才能让这个工作吗?

Is there something else that needs to be done to make this work under iOS 7?

推荐答案

对于发布解决方案的延迟,我深表歉意。在过去的几个月里我一直很忙,并且在我忘记这个帖子之前就被我的个人生活所困扰。以下是我用来从iOS 7设备获得连续位置的解决方案,无论它是在前台还是后台。

I apologise for delay in posting the solution. I have been busy in the last few months and got caught up with my personal life until I forgot about this thread. Here is the solution that I used to get continuous location from iOS 7 devices no matter it is in foreground or background.

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

You may find the full solution and explanation from blog and also github:-


  1. iOS 7和8的后台位置更新编程

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

方法和说明: -


  1. 我每隔1分钟重新启动一次位置管理员功能 didUpdateLocations

我允许位置管理员在关闭之前从设备获取位置10秒钟(以节省电池电量) )。

I allow the location manager to get the locations from the device for 10 seconds before shut it down (to save battery).

以下部分代码: -

Partial Code Below:-

-(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的后台位置更新编程,带有服务器讨论的位置更新

Update on May 2014: I got a few requests for adding sample codes on sending the location to server for a certain time interval. I have added the sample codes and also combined a fix for BackgroundTaskManager to solve a glitch for background running over an extended period of time. If you have any questions, you are welcomed to join us for a discussion here: Background Location Update Programming for iOS 7 with Location Update to Server Discussion

2015年1月更新:如果你想获得即使应用程序暂停,位置也会更新,请参阅:即使应用程序被暂停,如何获取iOS 7和8的位置更新

Update on January 2015: If you want to get the location update even when the app is suspended, please see: How to Get Location Updates for iOS 7 and 8 Even when the App is Suspended

这篇关于后台位置服务在iOS 7中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆