iOS 模拟器崩溃 requestAlwaysAuthorization()

标签 ios swift cllocationmanager

我的代码包含在下面。它是一个旨在返回当前位置坐标的类。我得到的唯一输出是“之前之后之前之后” - 围绕 requestAlwaysAuthorization 的打印行。然后应用程序崩溃。请求对话框有时会短暂显示,有时会持续几秒钟。有几次我什至可以按“确定”。该应用程序总是崩溃。我在 xCode 7.0 和现在的 xCode 7.0.1、iOS 9.0 中都遇到过这个问题。我在 StackOverflow 上到处搜索,关于这个主题的大多数问题都是针对 xCode 和 iOS 的早期版本,并且发布的解决方案对我的情况没有帮助。于是就有了这个问题。我还发现 YouTube 教程基本上可以完成我正在做的事情,但没有什么乐趣。我的 plist 中也有 NSLocationAlwaysUsageDescription,并且我有 Privacy - Location Usage Description 和 NSLocationWhenInUseUsageDescription 来作为很好的衡量标准。我还尝试通过 Product\Scheme 菜单从 xCode 发送位置,并且还尝试使用模拟器的 Debug\Location。我为每个尝试了几种不同的位置选项。模拟器的 map 应用程序似乎总是有效。 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

最佳答案

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

关于iOS 模拟器崩溃 requestAlwaysAuthorization(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33203712/

相关文章:

ios - CLLocationManager 在 IOS 上的奇怪行为?

ios - CL位置,地理编码器和区域搜索

ios - 在移动设备(iPad 设置屏幕)上滑动时,始终在右半部分滑动。如何在左半部分执行滑动

ios - 每次在 textField 中输入新值时如何更新 tableView 单元格的标签?

ios - 致命异常 : NSRangeException - index 2 beyond bounds [0 .。 1]

ios - 如何从 swift 2.1 中的对象数组创建字典?

ios - Swift 使用 realm.add() 或 realm.create() 将大量数据写入 Realm

ios - Apple MusicKit 开发者 token

ios - 禁用照片库访问的自动请求

ios - Apple 拒绝因为 UIBackgroundMode = location