2014-09-19 105 views
8

我剛剛更新到Xcode 6/iOS 8 SDK,我在模擬器中的位置服務模擬開始不起作用。我更新之前還好(我目前無法在真實設備上測試)。現在,當我選擇一個位置進行模擬時,沒有任何反應。代表的-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations方法未被調用。我重新啓動了Xcode,清理了構建文件夾,沒有任何改變。爲什麼會發生?Xcode 6/iOS 8位置模擬不起作用

回答

14

由於IOS 8在啓動CLLocationManager之前需要獲得授權。

你在調用這些方法嗎?

[self.locationManager requestWhenInUseAuthorization]; // For foreground access 
[self.locationManager requestAlwaysAuthorization]; // For background access 

如果您在XCode 6之前創建了項目,那麼您可能還需要爲新權限添加info.plist條目。

有關詳細信息看看這篇文章:Location Services not working in iOS 8

14

添加下面的代碼在你的方法

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) 
{ 
    [self.locationManager requestWhenInUseAuthorization]; 
} 

還要添加下面一行在你info.plist文件

重點:NSLocationWhenInUseUsageDescription值:用途當前位置

+0

有這個問題的任何人,我掙扎了一段時間,因爲我是用'[的LocationManager requestWhenInUseAuthorization]'的應用程序,但宣佈了'NSLocationAlwaysUsageDescription'中的.plist文件(注意區別「總是'和'在使用'這個解決方案,但確保你**聲明正確的用法描述類型**,NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription(或者兩者都有) – 1owk3y 2015-05-27 00:26:00

0

其他答案是正確的,但我也必須重置模擬器be前面我可以找到一個位置,但它在設備上工作正常。該應用程序最初安裝在該模擬器的iOS 8

之前要重置模擬器:

https://stackoverflow.com/a/16195931

2

使用的Xcode 6.3.1我有區位選擇停止更新。修復方法是運行另一個項目,選擇「模擬位置>不要模擬位置」,然後再次構建原始項目以恢復正常的位置設置。

0
- (void)startLocationUpdates{ 
    geocoder = [[CLGeocoder alloc] init]; 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 
     [locationManager requestWhenInUseAuthorization]; 
    [locationManager startUpdatingLocation]; 
} 

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

     CLLocation *currentLocation = newLocation; 
    if (currentLocation != nil) { 
    } 

    // Reverse Geocoding 
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { 

     if (error == nil && [placemarks count] > 0) { 
      placemark = [placemarks lastObject]; 
         fullAddress = [NSString stringWithFormat:@"%@,%@,%@,%@", 
          placemark.thoroughfare, 
          placemark.locality, 
          placemark.administrativeArea, 
          placemark.country]; 

      subtitleLocation = [NSString stringWithFormat:@"PostalCode::%@", 
           placemark.postalCode]; 
     } else { 
      // NSLog(@"%@", error.debugDescription); 
     } 
    } ]; 
} 

- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error{ 

    NSLog(@"Cannot find the location."); 
}