iOS模拟器崩溃requestAlwaysAuthorization()

问题描述:

我的代码包含在下面。这是一个旨在返回当前位置坐标的类。我得到的唯一输出是“before before after after” - 围绕requestAlwaysAuthorization的打印行。然后该应用程序崩溃。请求对话框有时会短暂显示,有时会显示几秒钟。在很少的情况下,我甚至可以按“OK”。该应用程序总是崩溃。我在xCode 7.0中遇到了这个问题,现在在这两种情况下都是xCode 7.0.1,iOS 9.0。我已经搜索了*的高低,关于这个主题的大部分问题都是针对xCode和iOS的早期版本的,而且我的案例中没有任何发布的解决方案对我有帮助。因此,这个问题。我也发现了YouTube教程,这些教程基本上做了我正在做的事情,但没有喜悦。我在我的plist中也有NSLocationAlwaysUsageDescription,并且我有隐私 - 位置使用描述和NSLocationWhenInUseUsageDescription用于很好的衡量。我也尝试通过Product \ Scheme菜单从xCode发送位置,并且我也尝试使用模拟器的Debug \ Location。我已经尝试了几种不同的位置选项。模拟器的地图应用程序似乎总是工作。而苹果的LocateMe(用Objective C编写)也适用。 Swift 2(我的代码如下)失败。iOS模拟器崩溃requestAlwaysAuthorization()

import CoreLocation 

class TheCurrentLocation: NSObject, CLLocationManagerDelegate { 

    var locationManager: CLLocationManager! 
    var latitude: Double = 0 
    var longitude: Double = 0 
    var locationStatus: NSString = "Not Started" 

    func initialize() { 

     self.locationManager = CLLocationManager() 
     self.locationManager.delegate = self 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer 
     print("before") 
     self.locationManager.requestAlwaysAuthorization() 
//  self.locationManager.requestWhenInUseAuthorization() 
     print("after") 

    } // END: initialize() 

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 

     self.locationManager.stopUpdatingLocation() 
     print("ERRORS: " + error.localizedDescription) 

    } // END: locationManager delegate didFailWithError 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     let location = locations.last 
     let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 
     self.locationManager.stopUpdatingLocation() 
     print ("ta da") 
     self.latitude = center.latitude 
     self.longitude = center.longitude 

    } // END: locationManager delegate didUpdateLocations 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 

     var isAllowed = false 

     switch status { 
     case CLAuthorizationStatus.Restricted: 
      locationStatus = "Restricted Access to Location" 
     case CLAuthorizationStatus.Denied: 
      locationStatus = "User Denied Access to Location" 
     case CLAuthorizationStatus.NotDetermined: 
      locationStatus = "Location Status Not Determined" 
     default: 
      locationStatus = "Allowed Access to Location" 
      isAllowed = true 
     } // END switch status 

     if (isAllowed == true) { 
      NSLog(String(locationStatus)) 
      self.locationManager.startUpdatingLocation() 
     } else { 
      NSLog(String(locationStatus)) 
     } 

    } // END: locationManager delegate didChangeAuthorizationStatus 

} // END: theCurrentLocation 
+2

更新您的问题与关于崩溃的细节。它在哪里崩溃,什么是完整和准确的错误信息? – rmaddy

+0

没有错误信息。我希望我能告诉你更多。除已经描述的印刷线之外,没有其他输出。 –

+0

@RobWelan在Xcode中,当你的应用程序崩溃时,你可以通过在调试器的'(lldb)'提示符下键入'bt'来获得一个报告崩溃错误的回溯。 – Daniel

我想我知道是什么原因造成的。你不能有NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription。只有其中一个。我想我之前遇到过这个问题。尝试删除其中一个,看看是否修复它。

+0

为什么要告诉用户在他们的问题表明他们都有这两个关键字时添加此密钥。更新你的答案。 – rmaddy

+0

我已经这样做了。 – mn1

+0

@ mn1:我已经删除了我的plist中的WhenInUsage和隐私项目(请参阅我的原始问题帖子,了解每个项目的全名)。没有帮助不幸。 –