CLLocationManagerDelegate

CLLocationManagerDelegate

//查看加载方法-已分配LocationManager并将CLLocationManagerDelegate包含在.h文件中

-ViewDidLoad{
self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    self.locationManager.distanceFilter=kCLDistanceFilterNone;
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager startMonitoringSignificantLocationChanges];
    [self.locationManager startUpdatingLocation];

}
// Location Manager Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"%@", [locations lastObject]);
}

最佳答案

在plist中,您必须添加2个条目

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

  • 将两者都设为“需要位置才能知道您在哪里”的字符串
    self.locationManager = [[CLLocationManager alloc]init];
    
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }
    self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];
    
    if (authorizationStatus == kCLAuthorizationStatusAuthorized ||
        authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
        authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {
        NSLog(@"You are authorized");
    
    }
    
    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];
    

    希望这可以帮助

    关于ios - 永远不会调用iOS 8 CLLocationManagerDelegate方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28837146/

    10-12 14:47