.notDetermined 等待用户响应
.notDetermined to wait for a response from user
我目前正在试验 CLLocation
,最近遇到了 authorizationStatus
。如果用户允许应用程序使用位置服务,我想显示一个视图,如果 he/she 不允许,我想显示另一个视图。它按我想要的方式工作,除了 .notDetermined
。如果我将它插入 .denied
-block 并按 "Allow when in use",我无法呈现 "allow" 页面,而是必须重新启动应用程序才能呈现它。如果我把它放在我的 .accesWhenInUse
块中并按 "deny",它也是相反的。所以我的问题是,使用此命令的最佳做法是什么?理想情况下,我希望应用程序等到用户实际做出选择并从那里加载它,但是由于需要 .notDetermined
来处理我有点迷路。
到目前为止我的初始位置功能:
private func initLocation() {
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse, .authorizedAlways :
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
break
case .notDetermined:
break
case .restricted, .denied:
print("well this is awkward..")
view.addSubview(noLocationTextView)
NSLayoutConstraint.activate([
noLocationTextView.heightAnchor.constraint(equalToConstant: 210),
noLocationTextView.widthAnchor.constraint(equalToConstant: 210),
noLocationTextView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
noLocationTextView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
])
noLocationTextView.text = "Well we're going to need your location"
break
}
}
}
我现在已经将 .notDetermined
提取到它自己的案例中。
所以问题是,我该如何处理这个问题,以便应用程序在做出选择时等待并加载正确的视图?
感谢@Paulw11,我设法使用 delegate method 解决了这个问题。所以我没有使用我的 initLocation()
,而是使用了这个:
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedWhenInUse, .authorizedAlways:
goToLocationSettings.removeFromSuperview()
noLocationContainer.removeFromSuperview()
break
case .denied, .restricted:
print("well this is awkward..")
view.addSubview(noLocationContainer)
NSLayoutConstraint.activate([
noLocationContainer.heightAnchor.constraint(equalTo: self.view.heightAnchor),
noLocationContainer.widthAnchor.constraint(equalTo: self.view.widthAnchor),
])
noLocationContainer.insertSubview(goToLocationSettings, at: 3)
NSLayoutConstraint.activate([
goToLocationSettings.widthAnchor.constraint(equalToConstant: 300),
goToLocationSettings.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
goToLocationSettings.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 200),
goToLocationSettings.heightAnchor.constraint(equalToConstant: 50)
])
break
default:
break
}
}
这种方式 .notDetermined
是 default
在用户做出选择之前什么都不做,就像我想要的那样。它还会实时更改,因此如果我拒绝访问位置然后在应用程序 运行 时授予它,它只会更新视图并显示内容。希望这对遇到类似问题的人有所帮助!
我目前正在试验 CLLocation
,最近遇到了 authorizationStatus
。如果用户允许应用程序使用位置服务,我想显示一个视图,如果 he/she 不允许,我想显示另一个视图。它按我想要的方式工作,除了 .notDetermined
。如果我将它插入 .denied
-block 并按 "Allow when in use",我无法呈现 "allow" 页面,而是必须重新启动应用程序才能呈现它。如果我把它放在我的 .accesWhenInUse
块中并按 "deny",它也是相反的。所以我的问题是,使用此命令的最佳做法是什么?理想情况下,我希望应用程序等到用户实际做出选择并从那里加载它,但是由于需要 .notDetermined
来处理我有点迷路。
到目前为止我的初始位置功能:
private func initLocation() {
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse, .authorizedAlways :
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
break
case .notDetermined:
break
case .restricted, .denied:
print("well this is awkward..")
view.addSubview(noLocationTextView)
NSLayoutConstraint.activate([
noLocationTextView.heightAnchor.constraint(equalToConstant: 210),
noLocationTextView.widthAnchor.constraint(equalToConstant: 210),
noLocationTextView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
noLocationTextView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
])
noLocationTextView.text = "Well we're going to need your location"
break
}
}
}
我现在已经将 .notDetermined
提取到它自己的案例中。
所以问题是,我该如何处理这个问题,以便应用程序在做出选择时等待并加载正确的视图?
感谢@Paulw11,我设法使用 delegate method 解决了这个问题。所以我没有使用我的 initLocation()
,而是使用了这个:
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedWhenInUse, .authorizedAlways:
goToLocationSettings.removeFromSuperview()
noLocationContainer.removeFromSuperview()
break
case .denied, .restricted:
print("well this is awkward..")
view.addSubview(noLocationContainer)
NSLayoutConstraint.activate([
noLocationContainer.heightAnchor.constraint(equalTo: self.view.heightAnchor),
noLocationContainer.widthAnchor.constraint(equalTo: self.view.widthAnchor),
])
noLocationContainer.insertSubview(goToLocationSettings, at: 3)
NSLayoutConstraint.activate([
goToLocationSettings.widthAnchor.constraint(equalToConstant: 300),
goToLocationSettings.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
goToLocationSettings.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 200),
goToLocationSettings.heightAnchor.constraint(equalToConstant: 50)
])
break
default:
break
}
}
这种方式 .notDetermined
是 default
在用户做出选择之前什么都不做,就像我想要的那样。它还会实时更改,因此如果我拒绝访问位置然后在应用程序 运行 时授予它,它只会更新视图并显示内容。希望这对遇到类似问题的人有所帮助!