我在我的iPhone应用程序中使用地理位置。即使我使用地理位置几秒钟,或者应用程序处于后台,电池也会消耗得更快。
如何检查我的iPhone设备上是否仍然启用了地理位置服务?这是我用过的代码:
...
locationManager = [[CLLocationManager alloc] init]; // Create new instance of locMgr
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSLog(@"locationManager startUpdatingLocation");
reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:locationManager.location.coordinate];
reverseGeocoder.delegate = self;
[reverseGeocoder start];
}
...
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
NSLog(@"reverseGeocoder failed: %@", [error localizedDescription]);
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
NSLog(@"locality %@", placemark.locality);
location = [placemark.locality retain];
}
答案 0 :(得分:1)
要查看并查看哪些应用使用了位置服务,请转到“设置”应用,在“位置服务”下,最近使用过该应用的应用旁会显示一个灰色箭头,并且正在使用该应用的应用旁边会显示一个紫色箭头
为了减少电池使用量,您应该在有足够好的位置满足需求时立即关闭CLLocationManager,并且只要您的应用程序处于活动状态:
-(void)appWillResignActive:(NSNotification*)notification
{
// pause GPS when not active to save battery
[locationManager stopUpdatingLocation];
}
在某处订阅通知,例如在视图控制器中:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appWillResignActive:)
name:UIApplicationWillResignActiveNotification object:nil];