简体   繁体   English

核心位置不在iOS模拟器上运行

[英]Core Location don't run on ios simulator

My App is created by Xcode 5 for ios7, when I pressed the button "Get Location" appeared the message "MyApp would like to use your current location", now i updated Xcode and on ios8 core location don't run, missing some key to add? 我的应用是由Xcode 5为ios7创建的,当我按下“获取位置”按钮时,出现了消息“ MyApp想要使用您的当前位置”,现在我更新了Xcode,并且在ios8核心位置上没有运行,缺少一些键加上? or some class? 还是上课? that in ios7 there was no need to implement? 在ios7中没有实现的必要吗?

iOS 8 CoreLocation API has changed . iOS 8 CoreLocation API已更改。

So the first thing you need to do is to add one or both of the following keys to your Info.plist file: 因此,您需要做的第一件事是将以下两个键中的一个或两个添加到Info.plist文件中:

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

Both of these keys take a string which is a description of why you need location services. 这两个键都带有一个字符串,该字符串描述了为什么需要定位服务。 You can enter a string like “Location is required to find out where you are” which, as in iOS 7, can be localized in the InfoPlist.strings file. 您可以输入一个字符串,例如“需要位置才能找到您所在的位置”,就像在iOS 7中一样,可以在InfoPlist.strings文件中进行本地化。

Next you need to request authorization for the corresponding location method, WhenInUse or Background. 接下来,您需要为相应的定位方法whenInUse或Background请求授权。 Use one of these calls: 使用以下调用之一:

[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]

Below code works both ios7 and ios8 . 下面的代码对ios7和ios8都适用。

if (_locationmanager == nil) {
    _locationmanager = [[CLLocationManager alloc] init];
}
_locationmanager.delegate = self;
_locationmanager.distanceFilter = kCLDistanceFilterNone;
if ([CLLocationManager  locationServicesEnabled]) {  // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.

if ([_locationmanager respondsToSelector:@selector(requestWhenInUseAuthorization)])           {
    [_locationmanager requestWhenInUseAuthorization];
}
[_locationmanager startUpdatingLocation];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM