Ste*_*eve 6 iphone cocoa-touch cllocationmanager ios
我想让我的应用程序继续在后台运行位置服务.为此我用过:
-(void)applicationDidEnterBackground:(UIApplication *)application {
[locationManager stopUpdatingLocation];
[locationManager startUpdatingLocation];
//timer=[NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(UpdateLocation) userInfo:nil repeats:YES];
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用NSTimer它时不会打电话UpdateLocation.我尝试使用另一种方法调用它,但之后它也只调用一次.
我想在后台持续运行应用程序,在定期间隔后检测位置.
我在我正在开发的应用程序中这样做了.当应用程序在后台但应用程序不断收到位置更新时,计时器不起作用.我在文档中的某处读过(我现在似乎无法找到它,当我这样做时会发布更新),当应用程序在后台时,只能在活动的运行循环上调用方法.即使在bg中,app委托也有一个活跃的运行循环,所以你不需要创建自己的循环来使这个工作.[我不确定这是否是正确的解释,但这就是我从我所读到的内容中理解的]
首先,在应用程序的info.plist中添加"背景模式"键的"位置"对象.现在,您需要做的是在应用中的任何位置启动位置更新:
CLLocationManager locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;//or whatever class you have for managing location
[locationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)
接下来,编写一个方法来处理位置更新,比如 - (void)didUpdateToLocation:(CLLocation*)位置,在app delegate中.然后在启动位置管理器的类中实现方法locationManager:didUpdateLocation:fromLocation of CLLocationManagerDelegate(因为我们将位置管理器委托设置为'self').在此方法中,您需要检查是否已经过了必须处理位置更新的时间间隔.您可以通过每次保存当前时间来完成此操作.如果该时间已过,请从您的app delegate调用方法UpdateLocation:
NSDate *newLocationTimestamp = newLocation.timestamp;
NSDate *lastLocationUpdateTiemstamp;
int locationUpdateInterval = 300;//5 mins
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (userDefaults) {
lastLocationUpdateTiemstamp = [userDefaults objectForKey:kLastLocationUpdateTimestamp];
if (!([newLocationTimestamp timeIntervalSinceDate:lastLocationUpdateTiemstamp] < locationUpdateInterval)) {
//NSLog(@"New Location: %@", newLocation);
[(AppDelegate*)[UIApplication sharedApplication].delegate didUpdateToLocation:newLocation];
[userDefaults setObject:newLocationTimestamp forKey:kLastLocationUpdateTimestamp];
}
}
}
Run Code Online (Sandbox Code Playgroud)
即使您的应用处于后台,这也会每隔5分钟调用您的方法.Imp:此实现耗尽电池,如果您的位置数据的准确性不重要,则应使用[locationManager startMonitoringSignificantLocationChanges]
在将此添加到您的应用程序之前,请阅读http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html上的位置感知编程指南.
| 归档时间: |
|
| 查看次数: |
7943 次 |
| 最近记录: |