【问题标题】:iOS app crashes on launch screen, location is requested after app crashes (Xcode 8.3.3)iOS 应用程序在启动屏幕上崩溃,应用程序崩溃后请求位置 (Xcode 8.3.3)
【发布时间】:2018-01-26 11:09:29
【问题描述】:

当我将我的 iOS 应用从 Xcode 运行到 iOS 模拟器或我的物理设备时,应用会在几秒钟内崩溃。当应用程序进入后台并且我返回到 iPhone 主屏幕时,会弹出询问是否允许使用我的位置的警报视图,但在我选择答案之前很快就消失了。

在声明并初始化一个名为“locationManager”的CLLocationManager后,我相信错误是由这些语句触发的:

    locationManager.requestWhenInUseAuthorization()
    locationManager.requestLocation()

控制台日志中出现的主要错误是:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didFailWithError:'

我已使用“NSLocationWhenInUseUsageDescription”设置我的使用说明,以便我的应用可以向用户展示他们的位置将用于什么,从而授予应用访问其位置的权限。我还有什么可能导致此错误的遗漏吗?

为了获取用户的位置,我是否只需要通过在 Info.plist 文件中添加“NSLocationWhenInUseUsageDescription”键来请求它,还是我需要采取更多措施?

【问题讨论】:

  • 有了这段代码,我怀疑有人可以帮助你
  • 有调试日志吗?
  • @kerry 我已将调试日志中的主要错误添加到帖子中:)
  • 您是否将代理设置为您的locationManager?你能发布它的代码吗?
  • @GIJOW 我尝试将课程设置为“CLLocationManagerDelegate”,但它告诉我我的课程does not conform to protocol 'NSObjectProtocol'。该类的代码来自 RxSwift,位于github.com/ReactiveX/RxSwift/blob/master/RxExample/RxExample/…

标签: ios xcode geolocation cllocationmanager rx-swift


【解决方案1】:

解决您的问题:

创建一个类,并重写方法如下:

class LocationDelegate : NSObject, CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
         // here you will receive your location updates on `locations` parameter

    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        // if something goes wrong comes to here
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
       // here you can monitor the authorization status changes
    }

}

在任意位置创建此类的全局实例。

let locationDelegate: LocationDelegate = LocationDelegate()

然后在您请求权限之前设置委托:

locationManager.delegate = locationDelegate

【讨论】:

    猜你喜欢
    • 2018-11-27
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    相关资源
    最近更新 更多