在加载 WKWebView 之前尝试获取 CLLocation 坐标
Trying to get CLLocation coordinates before loading WKWebView
基本上根据坐标,我得到州(NY、CA 等)并根据州访问特定的 url,我使用 WKWebView() 打开网站。
但是,当 webView 需要加载它时,我一直为 url 获取 nil。
如果我注释掉 webView.load(URLRequest(url: url!)))
,我注意到 url 值不是 nil(通过打印语句)。所以基本上,我需要在 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://whosebug.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)
}
}
我很茫然:(谢谢你的帮助。
与其在请求许可后立即启动 WKWebView
,不如在您实际拥有一个位置(即在委托中)后启动
func locationManager(CLLocationManager, didUpdateLocations: [CLLocation])
将是执行此操作的理想位置,只需确保只执行一次操作,因为将连续调用此委托方法。
或者,您也可以在 didChangeAuthorization
函数中执行此操作。
基本上根据坐标,我得到州(NY、CA 等)并根据州访问特定的 url,我使用 WKWebView() 打开网站。
但是,当 webView 需要加载它时,我一直为 url 获取 nil。
如果我注释掉 webView.load(URLRequest(url: url!)))
,我注意到 url 值不是 nil(通过打印语句)。所以基本上,我需要在 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://whosebug.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)
}
}
我很茫然:(谢谢你的帮助。
与其在请求许可后立即启动 WKWebView
,不如在您实际拥有一个位置(即在委托中)后启动
func locationManager(CLLocationManager, didUpdateLocations: [CLLocation])
将是执行此操作的理想位置,只需确保只执行一次操作,因为将连续调用此委托方法。
或者,您也可以在 didChangeAuthorization
函数中执行此操作。