繁体   English   中英

iOS应用在启动屏幕上崩溃,在应用崩溃后请求位置(Xcode 8.3.3)

[英]iOS app crashes on launch screen, location is requested after app crashes (Xcode 8.3.3)

当我从Xcode运行iOS应用到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”键来请求用户,还是需要采取其他措施?

解决您的问题:

创建一个类似的类,并重写以下方法:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM