代码之家  ›  专栏  ›  技术社区  ›  Moondra

在加载WKWebView之前尝试获取CLLocation坐标

  •  1
  • Moondra  · 技术社区  · 7 年前

    基本上基于坐标,我得到了状态(NY,CA等),并且基于状态,我访问了一个特定的url,我使用WKWebView()来打开网站。

    如果我把 webView.load(URLRequest(url: url!))) ,我注意到url值不是nil(通过print语句)。所以基本上,我需要在webView准备加载url之前获取坐标值。 我已经把点菜的事弄得乱七八糟了,但还是弄不好。

    import UIKit
    import WebKit
    import CoreLocation
    
    
    class ViewController: UIViewController,  WKNavigationDelegate,CLLocationManagerDelegate {
    
        var state: String?
    
        let locationManager = CLLocationManager()
        let translator = CLGeocoder()
        var webView: WKWebView!
    
    
        var locationsDict : [String:String] = ["CA" : "https://stackoverflow.com",
                                               "NY": "https://old.reddit.com"]
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
    
            locationManager.requestWhenInUseAuthorization()
            if CLLocationManager.locationServicesEnabled() {
                locationManager.delegate = self
                locationManager.desiredAccuracy = kCLLocationAccuracyKilometer // You can change the locaiton accuary here.
                locationManager.startUpdatingLocation()
            }
    
    
    
                webView = WKWebView()
                webView.navigationDelegate = self
                view = webView
    
    
    
                var url: URL?
                if state != nil{
                        let urlString = locationsDict[state!]
                        print(urlString)
                            if urlString != nil{
                                    url = URL(string: urlString!)!
    
                    }
                    }
            webView.load(URLRequest(url: url!))
            //webView.allowsBackForwardNavigationGestures = true
                print(state)
    
            }
    
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            if let location = locations.first {
    
                translator.reverseGeocodeLocation(location){(placemark, error) in
                    if error != nil{
                        print("There was an error")
                    }
                    else{
                        if let place = placemark?[0]
                        {
                            if let temp = place.administrativeArea{
                                    self.state = temp
                                print(self.state!)
                        }
    
                            else {print ("Cant find location")}
                    }
                }
    
    
    
    
    
            }
        }
        }
    
        // If we have been deined access give the user the option to change it
        func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
            if(status == CLAuthorizationStatus.denied) {
                showLocationDisabledPopUp()
            }
        }
    
    
    
    
        // Show the popup to the user if we have been deined access
        func showLocationDisabledPopUp() {
            let alertController = UIAlertController(title: "Background Location Access Disabled",
                                                    message: "In order to deliver pizza we need your location",
                                                    preferredStyle: .alert)
    
            let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
            alertController.addAction(cancelAction)
    
            let openAction = UIAlertAction(title: "Open Settings", style: .default) { (action) in
                if let url = URL(string: UIApplicationOpenSettingsURLString) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            }
            alertController.addAction(openAction)
    
            self.present(alertController, animated: true, completion: nil)
        }
    
    }
    

    我不知所措:(谢谢你的帮助。

    1 回复  |  直到 7 年前
        1
  •  1
  •   David Liaw    7 年前

    而不是启动 WKWebView

    func locationManager(CLLocationManager, didUpdateLocations: [CLLocation]) 这是一个理想的地方,只要确保只执行一次操作,因为这个委托方法将被连续调用。 或者,你也可以在 didChangeAuthorization

    推荐文章