2011-10-30 77 views
3

我在我的iPhone應用程序中使用地理位置。即使我已經使用了幾秒鐘的地理位置,或者應用程序在後臺,電池電量也會更快耗盡。iOS:如何檢查地理位置是否仍然打開?

如何檢查地理位置服務是否仍在我的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]; 
} 

回答

1

要檢查,看看應用程序正在使用的位置服務,去設置應用程序,在位置服務,會有一個灰色箭頭最近使用過的應用程序旁邊,以及正在使用它的應用程序旁邊的紫色箭頭。

enter image description here

爲了儘快降低電池的使用時,應關閉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]; 
相關問題