2016-03-07 40 views
-1

我正尝试将我的一个应用程序更新到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. 

不知道在这一点上该怎么做。我寻找类似的问题,但没有发现任何东西。

回答

1

的问题是这一行:

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

这种方法不会被调用,因为它对应于没有delegate method。您需要实施此方法:

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

非常感谢。完美工作。 – tombuarts

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]; 
} 

setupViewController删除您performselector方法,你必须使用所有的地方self。希望它能帮助你。

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>