2014-09-19 58 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."); 
}