我正在为iOS 8开发一个Swift代码。我正在尝试做一些涉及位置的事情,因此我在swift视图控制器文件中实现了以下内容:
let locationManger:CLLocationManager = CLLocationManager()
var speedReceived:Double = 0 override func viewDidLoad() { super.viewDidLoad() locationManger.delegate = self locationManger.desiredAccuracy = kCLLocationAccuracyBest let authstate = CLLocationManager.authorizationStatus() if(authstate == CLAuthorizationStatus.NotDetermined){ println("Not Authorised") locationManger.requestWhenInUseAuthorization() } // Do any additional setup after loading the view, typically from a nib. } func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){ var location:CLLocation = locations[locations.count - 1] as CLLocation if(location.horizontalAccuracy > 0){ self.speedReceived = location.speed println(self.speedReceived) } } func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) { println("Couldn't get your location") }
但是,我似乎无法使此代码正常工作。它不会保存我对位置使用的偏好。它甚至不提示我允许访问位置。我尝试更新我的info.plist。但这不起作用。顺便说一句,如果我始终在模拟器的隐私设置中选择,那么如果我立即切换回该应用程序,它将起作用。有人可以帮忙吗?我确定那是问题所在,因为我在控制台上获得了未授权。
有什么帮助吗?
这是与iOS 8相关的问题。您必须在文件中放入NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription键.plist(值可能是附加消息,将在位置警报中显示)。这些键在iOS 8中是必需的。
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
.plist
在Apple准则中怎么说:
当您使用CLLocationManager类的requestAlwaysAuthorization方法请求位置服务的授权时,此密钥是必需的。如果此密钥不存在,并且您调用了requestAlwaysAuthorization方法,则系统将忽略您的请求并阻止您的应用使用位置服务。