繁体   English   中英

请求位置时应用崩溃,而用户不执行任何操作

[英]App crashes when requesting location and user does nothing

我有一个按钮,它使用用户的位置在他们周围搜索。 如果他们尚未授予/拒绝使用其位置的权限,它将请求该权限

相关代码:

func updateLocationStatus(){
    locationEnabled = CLLocationManager.locationServicesEnabled()
    appLocationEnabled = CLLocationManager.authorizationStatus()
}

@IBAction func searchNearby(sender: UIButton) {
    updateLocationStatus()
    if appLocationEnabled == CLAuthorizationStatus.Denied {
        //Show an alert to let them know they've denied location permissions
        presentViewController(alert, animated: true, completion: nil)
    }
    else if appLocationEnabled == CLAuthorizationStatus.NotDetermined || 
        appLocationEnabled != CLAuthorizationStatus.AuthorizedWhenInUse &&
        appLocationEnabled != CLAuthorizationStatus.AuthorizedAlways{
        locationManager.requestWhenInUseAuthorization()
    }
    else{
        activityIndicator.startAnimating()
        locationManager.requestLocation()
    }
}

正常使用时效果很好。 但是,如果让“允许应用程序使用您的位置[不允许] [允许]”警报停留5秒钟,则该应用程序将崩溃。 该行跳到Xcode中导致错误是

locationManager.requestWhenInUseAuthorization()

并且提供的错误是

线程1:EXC_BAD_ACCESS(代码= 2,地址= 0xsomeaddress)

我什至不知道从哪里开始解决此问题,因为该错误告诉我什么也没有,并且该功能可以正常运行,除非您什么都不做太长时间

也许这将解决这个问题。 确保在您的Info.plist中添加适当的键值组合。

有点像这样:

在此处输入图片说明

另外,您可能想设置一个断点以确保已明确启用该位置。

要开始查找位置,您还应该配置位置管理器的desiredAccuracydistanceFilter属性,然后调用requestLocation()startUpdatingLocation()

回溯提供任何有用的信息吗? 在控制台中,您可以在Xcode中断时在提示符下键入bt。 或者,您可以看一下调试导航器。

暂无
暂无

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

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