【问题标题】:Track users position for a longer period of time长时间跟踪用户位置
【发布时间】:2013-12-04 18:40:40
【问题描述】:

我正在构建一个用作警报服务的应用程序。该应用每 10 分钟将有关用户位置的数据发送到我们的服务器。

问题是应用程序将在几个小时或几天后停止发送数据。我试图找到有关此的更多信息,并将我们的应用程序与持续跟踪数周的应用程序“Moves”的性能进行了比较。在他们的帮助部分,您可以了解跟踪失败的最常见原因 (http://www.moves-app.com/help),但这无济于事。

他们一定使用了一些我们没有使用的技巧。我猜他们使用了几种方法来实现这一点。

  • 任何人成功构建了具有类似功能的 iOS 应用程序,可以引导我走向正确的方向?

我的代码如下所示:

AppDelegate:

- (id)init
{
    self = [super init];
    if (self) {
        _locManager = [[MYLocationManager alloc] init];
    }

    return self;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self._locManager start];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self._locManager restart];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self._locManager restart];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    [self._locManager stop];
}

MYLocationManager:

- (void)start
{
    [self _invokeLocationUpdates];
}


- (void)stop
{
    [self _killLocationUpdates];
}

- (void)restart
{
    [self _killLocationUpdates];
    [self _invokeLocationUpdates];
}

- (void)_killLocationUpdates
{
    if (self._locationManager != nil) {
        [self._locationManager stopUpdatingLocation];    
        self._locationManager = nil;
    }
}


- (void)_invokeLocationUpdates
{
    if (self._locationManager == nil) {
        self._locationManager = [[CLLocationManager alloc] init];
        self._locationManager.delegate = self;

        self._locationManager.pausesLocationUpdatesAutomatically = NO;

        if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
            self._locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
            self._locationManager.distanceFilter = 10;
        }
        else {
            self._locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
            self._locationManager.distanceFilter = 100;
        }

        [self._locationManager startUpdatingLocation];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    // sending location info to server
    [[NSNotificationCenter defaultCenter] postNotificationName:MYSEC_NOTIFICATION_POSITION_UPDATE object:self];
}

在 info.plist 文件中我们有:

<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>

【问题讨论】:

    标签: ios objective-c gps location cllocationmanager


    【解决方案1】:

    您可以使用重大变化定位服务。基本上,如果您的应用程序被暂停或终止,当重大变化的位置服务接收到数据时,它会唤醒您的应用程序并给它一些时间来处理数据。你可以在Location and Maps Programming Guide阅读更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多