我在AppDelegate中獲取用戶的經度和緯度,它在我刪除時請求了一次權限\在模擬器上再次重新安裝後卸載應用程序它沒有要求權限,所以應用程序在這裏繼續崩潰是我的代碼Iphone模擬器沒有獲取位置
func initLocationManager() {
if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized){
seenError = false
locationFixAchieved = false
locationManager = CLLocationManager()
locationManager.delegate = self
// locationManager.locationServicesEnabled = true
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
locationManager.requestAlwaysAuthorization()
}
}
// Location Manager Delegate stuff
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationManager.stopUpdatingLocation()
if ((error) != nil) {
if (seenError == false) {
seenError = true
print(error)
}
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if (locationFixAchieved == false) {
locationFixAchieved = true
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as! CLLocation
var coord = locationObj.coordinate
LATITUDE = coord.latitude
LONGITUDE = coord.longitude
print(coord.latitude)
print(coord.longitude)
}
}
func locationManager(manager: CLLocationManager!,
didChangeAuthorizationStatus status: CLAuthorizationStatus) {
var shouldIAllow = false
switch status {
case CLAuthorizationStatus.Restricted:
locationStatus = "Restricted Access to location"
case CLAuthorizationStatus.Denied:
locationStatus = "User denied access to location"
case CLAuthorizationStatus.NotDetermined:
locationStatus = "Status not determined"
default:
locationStatus = "Allowed to location Access"
shouldIAllow = true
}
NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil)
if (shouldIAllow == true) {
NSLog("Location to Allowed")
// Start location services
locationManager.startUpdatingLocation()
} else {
NSLog("Denied access: \(locationStatus)")
}
}
所以它不會在模擬器上工作? –
它仍然沒有工作。我怎樣才能獲得用戶位置迅速 –
你在哪裏調用'requestWhenInUseAuthorization'? – Paulw11