2013-11-20 130 views
3

我正在构建一款应用程序,可用作警报服务。该应用每10分钟发送一次关于用户位置的数据到我们的服务器。跟踪用户长时间的位置

问题是应用程序将在几个小时或几天后停止发送数据。我试图找到更多关于此的信息,并将我们的应用程序与应用程序“移动”的性能进行了比较,该应用程序持续跟踪数周。在他们的帮助部分中,您可以了解跟踪失败的最常见原因(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

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