【发布时间】: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