当应用程序在后台时获取用户位置。 iOS [英] Getting user location when app is in background. IOS

查看:277
本文介绍了当应用程序在后台时获取用户位置。 iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在后台运行的应用,以获取用户的位置并将其通过http请求发送到服务器。我的初衷是每n分钟获取一次用户的位置,但是经过大量的研究和试验,我放弃了,因为ios在3分钟后杀死了我的后台任务。

I'm developing an app that works in the background to get the user's location and send it to server using http requests. My first intention was to get the user's location every n minutes, but after alot of research and trial, i gave up since ios kills my background tasks after 3 minutes.

然后我尝试进行MonitoringSignificantLocationChanges的工作,但是由于使用手机信号塔而导致的位置更新不准确,这破坏了我的应用程序的用途。

Then i tried working on MonitoringSignificantLocationChanges, but its inaccurate location updates due to cell tower usage ruins the purpose of my app.

对于以下任何一种解决方案,我们深表感谢:

A solution to any of the following is greatly appreciated:


  1. 每隔n分钟无限地获取用户在后台的位置。

  2. 在SignificantLocationChanges上以高精度获取用户的背景位置(使用gps)

  3. 任何其他具有高精度结果的后台解决方案。


推荐答案

这对我有用,我使用 CLLocationManagerDelegate ,在上注册更新didUpdateLocations 和应用程序委托

This is what works for me, I use CLLocationManagerDelegate, register for updates on didUpdateLocations and in app Delegate

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [_locationManager stopMonitoringSignificantLocationChanges];
    [_locationManager startUpdatingLocation];
}

我开始更新位置,对我来说,关键是应用程序何时转到背景我切换到重要位置更改,因此应用程序不会像这样浪费面糊:

I start updating location, the key for me, is when the app goes to background I switch to Significant Location Changes so the app doesn't drain the batter like this:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [_locationManager startMonitoringSignificantLocationChanges];
}

在didUpdateLocations中,您可以检查

In didUpdateLocations, you can check

BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
    isInBackground = YES;
}

并在后台启动任务以报告位置,例如

And start a task in the background to report the location for example

if (isInBackground) {
    [self sendBackgroundLocationToServer:self.location];
}

开始一项任务,希望对您有所帮助。

And start a task, I hope that helps.

这篇关于当应用程序在后台时获取用户位置。 iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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