我的代码包含在下面。这是一个用于返回当前位置坐标的类。我得到的唯一输出是“之前之后” - 围绕requestAlwaysAuthorization的打印行。然后App崩溃了。请求对话框有时会短暂显示,有时会持续几秒钟。有几次我甚至按“OK”。应用程序总是崩溃。我在xCode 7.0和现在的xCode 7.0.1,iOS 9.0中遇到过这个问题。我已经搜索了StackOverflow的高低,关于这个主题的大多数问题都是针对xCode和iOS的早期版本,并且在我的案例中没有一个发布的解决方案有帮助。因此,这个问题。我还发现YouTube教程基本上就是我正在做的事情,但没有快乐。我的plist中也有NSLocationAlwaysUsageDescription,我有隐私 - 位置使用说明和NSLocationWhenInUseUsageDescription以获得良好的衡量标准。我也尝试通过Product \ Scheme菜单从xCode发送位置,我也尝试使用模拟器的Debug \ Location。我为每个尝试了几个不同的位置选项。模拟器的地图应用似乎总是有效。 Apple的LocateMe(用Objective C编写)也有效。 Swift 2(我的代码如下)失败。
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
答案 0 :(得分:1)
我想我知道可能导致它的原因。您不能同时拥有NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription。只有其中一个。我想我之前遇到过这个问题。尝试删除其中一个并查看是否可以修复它。