跟踪用户较长时间的位置

时间:2013-11-20 16:35:15

标签: ios objective-c gps location cllocationmanager

我正在构建一个应用程序,它可用作警报服务。该应用每隔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>

1 个答案:

答案 0 :(得分:1)

您可以使用重大更改位置服务。基本上,如果您的应用程序被暂停或终止,当重要更改位置服务接收数据时,它会唤醒您的应用程序并为其提供一些时间来处理数据。您可以在Location and Maps Programming Guide了解更多信息。

相关问题