Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-确保访问位置坐标的正确方法?_Ios_Swift_Cllocationmanager_Location Services - Fatal编程技术网

iOS-确保访问位置坐标的正确方法?

iOS-确保访问位置坐标的正确方法?,ios,swift,cllocationmanager,location-services,Ios,Swift,Cllocationmanager,Location Services,在我的应用程序中,我当前使用的是一个webview,我传递了一个包含用户Lat/Lng的URL。当应用程序启动时,我startUpdatingLocation并聆听CLLocationManager委托方法didUpdateLocations,以使用此方法中提供的坐标初始化webview。有一个开关可以确保webview只初始化一次 问题是,这似乎不可靠——有时当从close启动应用程序时,不会触发此方法。这很奇怪,因为大多数时候它确实有效 我注意到,如果我在模拟器中更改位置,那么它将触发该方法

在我的应用程序中,我当前使用的是一个webview,我传递了一个包含用户Lat/Lng的URL。当应用程序启动时,我
startUpdatingLocation
并聆听
CLLocationManager
委托方法
didUpdateLocations
,以使用此方法中提供的坐标初始化webview。有一个开关可以确保webview只初始化一次

问题是,这似乎不可靠——有时当从close启动应用程序时,不会触发此方法。这很奇怪,因为大多数时候它确实有效

我注意到,如果我在模拟器中更改位置,那么它将触发该方法,但显然这是一种解决方法,而不是解决问题的方法

这让我觉得可能是因为位置没有改变,但是我怎么才能得到之前的位置坐标呢

如果有帮助的话,我很乐意发布一些代码片段

任何想法都将不胜感激——谢谢


要求的附加代码:

初始化位置管理器的功能:

func setupLocationManager() {
        locationManager!.delegate = self
        locationManager!.distanceFilter = kCLDistanceFilterNone
        locationManager!.desiredAccuracy = kCLLocationAccuracyBest
        locationManager!.startUpdatingLocation()
        print("Start updating location...")
}
loadView()中调用

当我发布代码时,下面是
didUpdateLocations()
方法:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let coords = manager.location?.coordinate
    if !webViewStarted {
        webViewStarted = true
        startWebView(coords!)
    } else {
        updateUserLocation(coords!)
    }
}
didChangeAuthorizationStatus
code:

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    var allow = false
    var message = ""
    switch status {
    case CLAuthorizationStatus.Restricted:
        message = "Restricted Access to location"
    case CLAuthorizationStatus.Denied:
        message = "User denied access to location"
    case CLAuthorizationStatus.NotDetermined:
        message = "Status not determined"
    default:
        print("Allowed to location Access")
        allow = true
    }

    if (allow == true) {
        setupLocationManager()
    } else {
        message = "\(message) \r\n This app requires location services to use"
        let alertView = UIAlertController(title: "Error getting location", message: message, preferredStyle: UIAlertControllerStyle.Alert)
        let cancel = UIAlertAction(title: "Okay", style: .Cancel, handler: nil)
        alertView.addAction(cancel)
        self.presentViewController(alertView, animated: true, completion: nil)
    }
}

你能在注册位置更新的地方发布代码吗?@RafałSroka刚刚更新了主帖子——谢谢你的关注!使用loadView而不是viewDidLoad有什么原因吗?似乎您希望等到视图和子视图初始化之后再更新它们。例如,他们可能手头有位置并将其传递给您的webView,但您的webView尚未加载。位置管理器可能会在您启动后立即为您提供坐标。放些木头进去。它总是在一开始就提供一些东西。@beyowulf在学习我使用的
WKWebView
时,出于某种原因,我还把位置设置的东西放在那里。我还有
viewDidLoad
,它做的更多,
loadView
目前只设置位置管理器和web视图配置。
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    var allow = false
    var message = ""
    switch status {
    case CLAuthorizationStatus.Restricted:
        message = "Restricted Access to location"
    case CLAuthorizationStatus.Denied:
        message = "User denied access to location"
    case CLAuthorizationStatus.NotDetermined:
        message = "Status not determined"
    default:
        print("Allowed to location Access")
        allow = true
    }

    if (allow == true) {
        setupLocationManager()
    } else {
        message = "\(message) \r\n This app requires location services to use"
        let alertView = UIAlertController(title: "Error getting location", message: message, preferredStyle: UIAlertControllerStyle.Alert)
        let cancel = UIAlertAction(title: "Okay", style: .Cancel, handler: nil)
        alertView.addAction(cancel)
        self.presentViewController(alertView, animated: true, completion: nil)
    }
}