首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当应用程序处于后台时,获取用户位置。IOS

当应用程序处于后台时,获取用户位置。IOS
EN

Stack Overflow用户
提问于 2015-06-19 14:18:03
回答 2查看 568关注 0票数 2

我正在开发一个在后台工作的应用程序,它可以获取用户的位置并使用http请求将其发送到服务器。我的第一个目的是每隔n分钟获得用户的位置,但是经过大量的研究和试验,我放弃了,因为ios在3分钟后就杀死了我的后台任务。

然后我尝试在MonitoringSignificantLocationChanges上工作,但是由于手机塔的使用,它的不准确的位置更新破坏了我的应用程序的用途。

我们非常赞赏对下列任何一项的解决办法:

  1. 每n分钟无限地获取用户在后台的位置。
  2. 在SignificantLocationChanges上高精度地获取用户的背景定位(用gps)
  3. 任何其他高精度的背景解决方案。
EN

回答 2

Stack Overflow用户

发布于 2015-10-25 03:17:48

这就是我要做的,我使用CLLocationManagerDelegate,寄存器在didUpdateLocations上和应用程序中进行更新。

代码语言:javascript
运行
复制
- (void)applicationDidBecomeActive:(UIApplication *)application {
    [_locationManager stopMonitoringSignificantLocationChanges];
    [_locationManager startUpdatingLocation];
}

我开始更新位置,对我来说,关键是当应用程序转到后台时,我切换到重要的位置更改,这样应用程序就不会像这样耗尽面糊:

代码语言:javascript
运行
复制
- (void)applicationDidEnterBackground:(UIApplication *)application {
    [_locationManager startMonitoringSignificantLocationChanges];
}

在didUpdateLocations中,您可以检查

代码语言:javascript
运行
复制
BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
    isInBackground = YES;
}

并在后台启动一个任务来报告位置,例如

代码语言:javascript
运行
复制
if (isInBackground) {
    [self sendBackgroundLocationToServer:self.location];
}

开始一项任务,我希望这会有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2015-06-19 14:33:19

在SignificantLocationChanges上高精度地获取用户的背景定位(用gps)

做以下工作:

info.plist中添加以下内容

代码语言:javascript
运行
复制
    <key>NSLocationAlwaysUsageDescription</key>
    <string>{your app name} requests your location coordinates.</string>
    <key>UIBackgroundModes</key>
    <array>
        <string>location</string>
    </array>

在代码中,使用LoctionManager获取位置更新,(它将在前景和背景中工作)

代码语言:javascript
运行
复制
@interface MyViewController <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end 

@implementation MyViewController
-(void)startLocationUpdates {
    // Create the location manager if this object does not
    // already have one.
    if (self.locationManager == nil) {
        self.locationManager = [[CLLocationManager alloc] init];
    }

    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    self.locationManager.activityType = CLActivityTypeFitness;

    // Movement threshold for new events.
    self.locationManager.distanceFilter = 25; // meters

    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    }
    [self.locationManager startUpdatingLocation];
}

- (void)stopLocationUpdates {
    [self.locationManager stopUpdatingLocation];
}

#pragma mark CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    // Add your logic here
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"%@", error);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30940451

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档