CLLocationManager 使我的应用程序在激活位置后崩溃
CLLocationManager make my app crash having location activated
这很奇怪。有些设备会崩溃,有些设备不会。问题是当位置未激活时,该应用程序永远不会死,但是当我允许我的应用程序访问该位置时,某些设备崩溃而其他设备则不会。
这是代码:
override func viewDidAppear(animated: Bool) {
if CLLocationManager.locationServicesEnabled(){
switch CLLocationManager.authorizationStatus() {
case .NotDetermined, .Restricted, .Denied:
print("No access")
case .AuthorizedAlways, .AuthorizedWhenInUse:
let geocoder = CLGeocoder()
longitude = self.locationManager.location!.coordinate.longitude
latitude = self.locationManager.location!.coordinate.latitude
geocoder.reverseGeocodeLocation(CLLocation(latitude: (latitude), longitude: (longitude)), completionHandler: {placemarks, error in
if error == nil && placemarks!.count > 0 {
self.thoroughfare = (placemarks!.last?.thoroughfare)!
self.city = (placemarks!.last?.locality)!
print(self.thoroughfare)
print(self.city)
print(self.longitude)
print(self.latitude)
}
})
}
} else {
print("Location services are not enabled")
}
}
当应用程序崩溃时,错误指向此行:
longitude = self.locationManager.location!.coordinate.longitude
latitude = self.locationManager.location!.coordinate.latitude
我已经在 10 台设备上测试了该应用程序,此时有 1-2 台设备崩溃了。
发生了什么事?我认为我正在正确地管理在允许或不允许位置时做什么和不做什么。
你应该检查一下
self.locationManager.location
使用前为空
不要为变量声明可选值。始终处理错误
不要打开位置
self.locationManager.location // 你的错误
if var longitude = self.locationManager.location.coordinate.longitude {
// do your thing
}else {
// handle the error by declaring default value
}
第二件事你也可能会收到空值,即使用户在获取位置时丢失了互联网或者你在模拟器中测试时忘记了模拟位置所以总是处理错误
请检查您的 didUpdateLocations 方法,您只需要检查 location 是获取正确还是获取 nil。
if ((self.locationManager.location) != nil){
//在此处获取位置访问权限。
}
请尝试使用这整个代码来获取位置及其 details.Its 在 Swift 3.0
中尝试过且有效的解决方案
import CoreLocation
import Foundation
class ViewController: UIViewController,CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
findMyLocation()
}
func findMyLocation(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
if (error != nil) {
print("Reverse geocoder failed with error" + error!.localizedDescription)
return
}
if placemarks!.count > 0 {
let pm = placemarks![0]
self.displayLocationInfo(pm)
} else {
print("Problem with the data received from geocoder")
}
})
}
func displayLocationInfo(_ placemark: CLPlacemark?) {
if let containsPlacemark = placemark {
//stop updating location to save battery life
locationManager.stopUpdatingLocation()
let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : ""
let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : ""
let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : ""
let country = (containsPlacemark.country != nil) ? containsPlacemark.country : ""
print(" Postal Code \(postalCode)")
print(" administrativeArea \(administrativeArea)")
print(" country \(country)")
print(" locality \(locality)")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error while updating location " + error.localizedDescription)
}
谢谢
这很奇怪。有些设备会崩溃,有些设备不会。问题是当位置未激活时,该应用程序永远不会死,但是当我允许我的应用程序访问该位置时,某些设备崩溃而其他设备则不会。
这是代码:
override func viewDidAppear(animated: Bool) {
if CLLocationManager.locationServicesEnabled(){
switch CLLocationManager.authorizationStatus() {
case .NotDetermined, .Restricted, .Denied:
print("No access")
case .AuthorizedAlways, .AuthorizedWhenInUse:
let geocoder = CLGeocoder()
longitude = self.locationManager.location!.coordinate.longitude
latitude = self.locationManager.location!.coordinate.latitude
geocoder.reverseGeocodeLocation(CLLocation(latitude: (latitude), longitude: (longitude)), completionHandler: {placemarks, error in
if error == nil && placemarks!.count > 0 {
self.thoroughfare = (placemarks!.last?.thoroughfare)!
self.city = (placemarks!.last?.locality)!
print(self.thoroughfare)
print(self.city)
print(self.longitude)
print(self.latitude)
}
})
}
} else {
print("Location services are not enabled")
}
}
当应用程序崩溃时,错误指向此行:
longitude = self.locationManager.location!.coordinate.longitude
latitude = self.locationManager.location!.coordinate.latitude
我已经在 10 台设备上测试了该应用程序,此时有 1-2 台设备崩溃了。
发生了什么事?我认为我正在正确地管理在允许或不允许位置时做什么和不做什么。
你应该检查一下
self.locationManager.location
使用前为空
不要为变量声明可选值。始终处理错误 不要打开位置
self.locationManager.location // 你的错误
if var longitude = self.locationManager.location.coordinate.longitude {
// do your thing
}else {
// handle the error by declaring default value
}
第二件事你也可能会收到空值,即使用户在获取位置时丢失了互联网或者你在模拟器中测试时忘记了模拟位置所以总是处理错误
请检查您的 didUpdateLocations 方法,您只需要检查 location 是获取正确还是获取 nil。
if ((self.locationManager.location) != nil){
//在此处获取位置访问权限。
}
请尝试使用这整个代码来获取位置及其 details.Its 在 Swift 3.0
中尝试过且有效的解决方案import CoreLocation
import Foundation
class ViewController: UIViewController,CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
findMyLocation()
}
func findMyLocation(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in
if (error != nil) {
print("Reverse geocoder failed with error" + error!.localizedDescription)
return
}
if placemarks!.count > 0 {
let pm = placemarks![0]
self.displayLocationInfo(pm)
} else {
print("Problem with the data received from geocoder")
}
})
}
func displayLocationInfo(_ placemark: CLPlacemark?) {
if let containsPlacemark = placemark {
//stop updating location to save battery life
locationManager.stopUpdatingLocation()
let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : ""
let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : ""
let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : ""
let country = (containsPlacemark.country != nil) ? containsPlacemark.country : ""
print(" Postal Code \(postalCode)")
print(" administrativeArea \(administrativeArea)")
print(" country \(country)")
print(" locality \(locality)")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error while updating location " + error.localizedDescription)
}
谢谢