2016-03-07 39 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>