位置管理员没有获得位置

时间:2016-03-07 03:29:10

标签: objective-c cllocationmanager

我正在尝试将我的某个应用更新为IOS9和Xcode 7.2.1。位置管理器工作在早期版本,但现在无法正常工作。没有错误或警告......没有。看来我的代码没有进入“didUpdateLocations”。我的代码如下;

- (void)setupViewController {

    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
    {
        [self.locationManager requestAlwaysAuthorization];
    }

    locationManager.desiredAccuracy = 100;
    //locationManager.requestLocation;
    [locationManager startUpdatingLocation];


    //return coordinate;

    [self performSelector:@selector(stopUpdatingLocation:) withObject:@"Timed Out" afterDelay:5];       

}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"Doesn't get here.");

    self.bestEffortAtLocation = newLocation;
    latitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
    longitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];

There is other code here that gets city, country, etc.

目前不知道该怎么做。我找了类似的问题而没有找到任何东西。

3 个答案:

答案 0 :(得分:1)

问题在于这一行:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateLocations:(CLLocation *)newLocation 
    fromLocation:(CLLocation *)oldLocation {

永远不会调用该方法,因为它对应于no delegate method。您需要实现此方法:

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations {

答案 1 :(得分:1)

更新您的info.plist文件。

<key>NSLocationWhenInUseUsageDescription</key>
<string>This application requires location services to work</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>

答案 2 :(得分:0)

尝试这样。

- (void)setupViewController
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = 100;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

    [self.locationManager startUpdatingLocation];
    [self.locationManager requestWhenInUseAuthorization];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    self.bestEffortAtLocation = newLocation;
    latitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
    longitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];

    [self.locationManager stopUpdatingLocation];
}

performselector移除setupViewController方法,您必须使用所有地点self。希望它能帮到你。