2014-10-04 33 views
2

我的应用程序由Xcode的5 ios7,当我按下按钮“获取位置”出现消息“MyApp的想用你的当前位置”,现在我更新Xcode和创建运行在ios8核心位置不运行,缺少一些关键要添加?还是某个班?在ios7中没有必要实现?核心位置并不在iOS模拟器

回答

3

的iOS 8 CoreLocation API已经改变。

所以,你需要做的第一件事就是添加下列键的一个或两个的Info.plist文件:

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

这两个键的一个字符串这是您需要位置服务的原因的描述。你可以像“是必需的位置,找出你在哪里”,正如iOS中7,可在InfoPlist.strings文件本地化输入一个字符串。

接下来,需要请求授权用于对应的位置的方法,或WhenInUse背景。使用以下电话之一:

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

下面的代码适用于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]; 
+0

完美!!!!跑!!!!!非常感谢你 – 2014-10-06 14:30:42

+0

谢谢你梦幻般的回答:) – Andy 2015-03-23 20:21:04