【问题标题】:CLLocationManager make my app crash having location activatedCLLocationManager 使我的应用程序在激活位置时崩溃
【发布时间】:2017-01-12 15:55:39
【问题描述】:

这很奇怪。有一些设备会崩溃,而其他一些设备不会。问题是当没有激活​​位置时,应用程序永远不会死,但是当我允许我的应用程序访问某些设备中的位置时会崩溃,而在其他设备中则不会。

这是代码:

    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 台在此时崩溃。

发生了什么事?我认为我正确地管理了在允许或不允许位置时做什么和不做什么。

【问题讨论】:

    标签: ios swift crash cllocationmanager


    【解决方案1】:

    你应该检查一下

    self.locationManager.location
    

    使用前为空

    【讨论】:

    • 做到了。该应用程序不再崩溃但为什么有时为零,有时不是零,允许位置?
    • 因为允许位置并不意味着关于用户位置的数据已经存在。
    • 这是为什么呢?我认为当您允许应用程序访问用户位置时,您将获得一个坐标。为什么有些设备的 locationManager 为 nil 而有些则没有?
    • 用户可能在飞机上或地下或其他无法确定位置的地方。仅仅因为您有权访问设备位置并不意味着设备知道它在哪里。
    【解决方案2】:

    请尝试整个代码以获取位置及其详细信息。它在 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)
        }
    

    谢谢

    【讨论】:

      【解决方案3】:

      不要为变量声明可选值。总是处理错误 不要打开位置

      self.locationManager.location // 你的错误

        if var longitude = self.locationManager.location.coordinate.longitude {
             // do your thing
          }else {
         // handle the error by declaring default value
      }
      

      第二件事你也可能会收到空值,即使用户在获取位置时失去了互联网,或者你在模拟器中测试时忘记了模拟位置,所以总是处理错误

      【讨论】:

        【解决方案4】:

        请检查您的 didUpdateLocations 方法,您只需要检查位置是否正确或为零。

        if ((self.locationManager.location) != nil){

        //在此处获取位置访问权限。

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-09
          相关资源
          最近更新 更多