OGeek|极客世界-中国程序员成长平台

标题: ios - CLLocationManager.authorizationStatus 导致 EXC_BAD_ACCESS 代码=2 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:14
标题: ios - CLLocationManager.authorizationStatus 导致 EXC_BAD_ACCESS 代码=2

如果允许定位服务,我的第一个应用到目前为止运行良好。

一旦我特别禁用此应用的位置服务(飞行模式以及通常禁用的位置服务按预期工作)。

The Error at runtime

代码如下:

    func locationServices()->Bool{
    if CLLocationManager.locationServicesEnabled() {
        switch(CLLocationManager.authorizationStatus()) {
        case .NotDetermined, .Restricted, .Denied:
            return false
        case .AuthorizedAlways, .AuthorizedWhenInUse:
            return true
        }
    } else {
        return false
    }

}

从 viewDidLoad 调用:

    override func viewDidLoad() {
    super.viewDidLoad()

    txtNotes.delegate = self
    datePicker.maximumDate = NSDate()
    if (inc != nil) {
        let Dateformatter = NSDateFormatter()
        Dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle
        navigationItem.title = Dateformatter.stringFromDate((inc?.date)!)
        datePicker.date = (inc?.date)!
        txtNotes.text = inc?.notes
        ratingControl.rating = (inc?.rating)!
        lblAccuracy.text = inc?.geoloc
        self.location = inc?.geocor
    }
    else {
        if(locationServices()){
        self.locationManager = CLLocationManager()
        locationManager!.delegate = self
        locationManager!.requestWhenInUseAuthorization()
        locationManager!.desiredAccuracy = kCLLocationAccuracyBest
        locationManager!.startUpdatingLocation()

        }
        else{
            lblAccuracy.text = "Location Services Disabled"
            let alertController = UIAlertController(
                title: "Location Services Disabled",
                message: "lease allow this application to use location services",
                preferredStyle: .Alert)

            let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
            alertController.addAction(cancelAction)

            let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
                if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
                    UIApplication.sharedApplication().openURL(url)
                }
            }
            alertController.addAction(openAction)
            self.presentViewController(alertController, animated: true, completion: nil)
            viewDidLoad()
        }
    }
}



Best Answer-推荐答案


访问尚未初始化的委托(delegate)不是一个聪明的主意...

在访问它之前进行初始化解决了我的问题。

关于ios - CLLocationManager.authorizationStatus 导致 EXC_BAD_ACCESS 代码=2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38912678/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4