仅在模拟器 iPhone6 中首次启动时出现位置权限问题 - XCode 9.4.1

Posted

技术标签:

【中文标题】仅在模拟器 iPhone6 中首次启动时出现位置权限问题 - XCode 9.4.1【英文标题】:Problem with location permission during first time launch only in simulator iPhone6 - XCode 9.4.1 【发布时间】:2018-08-30 22:02:36 【问题描述】:

我有一个奇怪的问题。 该问题仅出现在 iPhone6 模拟器中。 当我第一次启动应用程序时,未经许可,然后在此代码中显示失败。此代码位于主 ViewController 中的 ViewDidLoad

manager.delegate = self
manager.requestWhenInUseAuthorization()
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
var curLoc:CLLocation!
curLoc = manager.location
mapView.delegate = self
if (isLocationPermissionGranted() == false)
         MapView.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 52.406464, longitude: 16.924997), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)), animated: true)
        else
        
         MapView.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake(curLoc.coordinate.latitude, curLoc.coordinate.longitude), MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)), animated: true)
        

        let getJSON = JSONDownload()
        getJSON.JSONDownloader(MapView: MapView)
    

在 else 块中我有错误

线程 1:致命错误:打开包装时意外发现 nil 可选值

但在任何其他模拟器或我的手机(iPhone 6s,ios 11.4.1)中只显示

无法从角落 4 插入法律归属

关于这条消息,我也有点困惑,因为,我想,我拥有所有权限选项。

我的许可内容是:

在 Info.plist 中

隐私 - 使用时的位置使用说明

隐私 - 位置使用说明

在 ViewController 的代码中

let manager = CLLocationManager()

此外,我还可以防止本地化错误:

func isLocationPermissionGranted() -> Bool
guard CLLocationManager.locationServicesEnabled() else
    return false

return [.authorizedAlways, .authorizedWhenInUse].contains(CLLocationManager.authorizationStatus())

有机会解决吗? :) 感谢您的回答! :)

【问题讨论】:

MapView(按照惯例应该是mapView)还是@IBOutlet?或以其他方式隐式展开的可选? - 似乎在您调用此代码时MapViewnil 是的,我有@IBOutlet weak var MapView: MKMapView!一切正常,例如显示注释或显示当前位置。我学习了约定,对未来很有用,谢谢:) 那么,您在哪里运行该代码? viewWillAppear? viewDidLoad? viewDidLoad。我有manager.desiredAccuracy = kCLLocationAccuracyBestmanager.startUpdatingLocation()var curLoc:CLLocation!curLoc = manager.locationmapView.delegate = self,在代码块之后有错误:) 您应该编辑您的问题以添加该上下文和代码,因为它很重要。 【参考方案1】:

在您的代码中,您将 curLoc 声明为隐式展开的可选项,然后将 manager.location 分配给它;但manager.location 是可选的,可能是nillocation 可能是 nil 的原因有很多;设备确定其位置需要时间,否则用户可能已拒绝访问位置。

无论是什么原因,当您随后访问包含nilcurLoc 时,您会得到一个异常,因为隐式展开的可选项的合同是它不会是nil

您需要安全地解开manager.location 以避免崩溃。

mapView.delegate = self

if let curLoc = manager.location, isLocationPermissionGranted()          
    MapView.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake(curLoc.coordinate.latitude, curLoc.coordinate.longitude), MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)), animated: true)
 else 
    MapView.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 52.406464, longitude: 16.924997), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)), animated: true)

【讨论】:

我更改了它,模拟器没有崩溃,但我仍然有错误Could not inset legal attribution from corner 4 你可以忽略它;这只是 MapKit 的一些日志记录。

以上是关于仅在模拟器 iPhone6 中首次启动时出现位置权限问题 - XCode 9.4.1的主要内容,如果未能解决你的问题,请参考以下文章

仅在 iOS 移动 Safari 中上传图像时出现错误的 CORS 失败

在 Visual Studio 2019 中首次启动时,Android 模拟器未显示在调试菜单中

如何在棉花糖中首次启动应用程序时获取位置访问权限

图库灯箱仅在 IE11 中首次单击时不插入图像

iPhone HelpViewController 未声明(本函数中首次使用)

在 iPhone6 设备上运行我的应用程序时出现错误