菜鸟教程小白 发表于 2022-12-11 17:14:43

ios - CLLocationManager.authorizationStatus 导致 EXC_BAD_ACCESS 代码=2


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

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

<p> <a href="/image/hg227.png" rel="noreferrer noopener nofollow"><img src="/image/hg227.png" alt="The Error at runtime"/></a> </p>

<p>代码如下:</p>

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

}
</code></pre>

<p>从 viewDidLoad 调用:</p>

<pre><code>    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 = &#34;Location Services Disabled&#34;
            let alertController = UIAlertController(
                title: &#34;Location Services Disabled&#34;,
                message: &#34;Please allow this application to use location services&#34;,
                preferredStyle: .Alert)

            let cancelAction = UIAlertAction(title: &#34;Cancel&#34;, style: .Cancel, handler: nil)
            alertController.addAction(cancelAction)

            let openAction = UIAlertAction(title: &#34;Open Settings&#34;, 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()
      }
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>访问尚未初始化的委托(delegate)不是一个聪明的主意...</p>

<p>在访问它之前进行初始化解决了我的问题。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - CLLocationManager.authorizationStatus 导致 EXC_BAD_ACCESS 代码=2,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38912678/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38912678/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - CLLocationManager.authorizationStatus 导致 EXC_BAD_ACCESS 代码=2