Xcode 6/iOS 8 位置模拟不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25935093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 6/iOS 8 Location Simulation doesn't work
提问by Can Poyrazo?lu
I've just updated to Xcode 6/iOS 8 SDK and my location service simulation in simulator started not working. It was fine before I updated (I'm currently unable to test on real device). Now, when I select a location for simulation, nothing happens. Delegate's -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
method is not called. I've restarted Xcode, cleaned the build folder, nothing changed. Why can this happen?
我刚刚更新到 Xcode 6/iOS 8 SDK 并且我在模拟器中的位置服务模拟开始不起作用。在我更新之前还好(我目前无法在真实设备上进行测试)。现在,当我选择一个位置进行模拟时,什么也没有发生。-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
不调用委托的方法。我重新启动了 Xcode,清理了构建文件夹,没有任何改变。为什么会发生这种情况?
回答by Prine
Since IOS 8 you need to ask for authorization before starting the CLLocationManager.
从 IOS 8 开始,您需要在启动 CLLocationManager 之前请求授权。
Are you calling one of these methods?
您正在调用这些方法之一吗?
[self.locationManager requestWhenInUseAuthorization]; // For foreground access
[self.locationManager requestAlwaysAuthorization]; // For background access
If you have created the project before XCode 6, you probably also need to add the info.plist entry for the new permission.
如果您在 XCode 6 之前创建了项目,您可能还需要为新权限添加 info.plist 条目。
For more details have a look at this post: Location Services not working in iOS 8
有关更多详细信息,请查看此帖子:定位服务在 iOS 8 中不起作用
回答by NaveenReddy
Add Below code in your method
在您的方法中添加以下代码
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
Also add Below line at your info.plist
file
还要在您的info.plist
文件中添加以下行
Key:NSLocationWhenInUseUsageDescription
value:Uses current location
键:NSLocationWhenInUseUsageDescription
值:使用当前位置
回答by Tr0yJ
Using Xcode 6.3.1 I have had the location selection stop updating. The fix was to run another project, select "Simulate location > Don't Simulate Location" then build the original project again to resume normal location setting.
使用 Xcode 6.3.1 我的位置选择停止更新。修复方法是运行另一个项目,选择“模拟位置 > 不模拟位置”,然后再次构建原始项目以恢复正常的位置设置。
回答by Mannam Brahmam
- (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.");
}
回答by alex bird
The other answers are correct, but I also had to reset the simulator before I could get a location, whereas it was working fine on a device. The app was initially installed on that simulator before iOS 8.
其他答案是正确的,但我还必须在获得位置之前重置模拟器,而它在设备上运行良好。该应用程序最初安装在 iOS 8 之前的模拟器上。
To reset your simulator :
重置你的模拟器: