我一直在开发我的位置感知应用程序。 我正在尝试从 View Controller 中提取位置权限流。这样我就有了更简洁的代码。
我写了 LocationPermission 类:
struct LocationPermission {
private init() { }
static func locationPermissionManager(with locationAuthStatus: CLAuthorizationStatus, viewController: UIViewController) {
switch locationAuthStatus {
case .notDetermined:
CLLocationManager().requestAlwaysAuthorization()
case .denied, .restricted:
Amplitude.instance()?.logEvent("Location Service: either denied or restricted")
showLocationAceessDeniedAlert(on: viewController)
case .authorizedAlways:
Amplitude.instance()?.logEvent("Location Service: Authorized Always")
case .authorizedWhenInUse:
Amplitude.instance()?.logEvent("Location Service: Authorized when in use")
}
}
static func showLocationAceessDeniedAlert(on viewController: UIViewController) {
let alertController = UIAlertController(title: "Permission update", message: "📍Location service needs to be enabled from settings.", preferredStyle: .alert)
let settingAction = UIAlertAction(title: "Open Settings", style: .default) { alertAction in
if let appSettings = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(appSettings, options: [:], completionHandler: nil)
}
}
alertController.addAction(settingAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
viewController.present(alertController, animated: true, completion: nil)
}
}
然后我觉得扩展更干净优雅。
所以我也在CLLocationManager
extension CLLocationManager {
static func locationPermissionManager(with locationAuthStatus: CLAuthorizationStatus, viewController: UIViewController? = nil) {
switch locationAuthStatus {
case .notDetermined:
CLLocationManager().requestAlwaysAuthorization()
case .denied, .restricted:
Amplitude.instance()?.logEvent("Location Service: either denied or restricted")
if let viewController = viewController {
self.init().showLocationAceessDeniedAlert(on: viewController, message: "📍Location service needs to be enabled from settings.")
}
case .authorizedAlways:
Amplitude.instance()?.logEvent("Location Service: Authorized Always")
case .authorizedWhenInUse:
if let viewController = viewController {
self.init().showLocationAceessDeniedAlert(on: viewController, message: "📍Location service: Please select 'Always' under location access")
}
Amplitude.instance()?.logEvent("Location Service: Authorized when in use")
}
}
private func showLocationAceessDeniedAlert(on viewController: UIViewController, message: String) {
let alertController = UIAlertController(title: "Permission update needed", message: message, preferredStyle: .alert)
let settingAction = UIAlertAction(title: "Open Settings", style: .default) { alertAction in
if let appSettings = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(appSettings, options: [:], completionHandler: nil)
}
}
alertController.addAction(settingAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
viewController.present(alertController, animated: true, completion: nil)
}
}
我都在按预期工作。我现在很困惑哪个是更好的解决方案。
在此请教各位高手。根据编码标准,哪种解决方案是更好的解决方案?
最佳答案
如果代码与目标类或结构广泛相关,则扩展很有用。
查看代码,它显然与 UIViewController
相关,而不是与 CLLocationManager
相关。
怎么样,静态方法改成实例方法了:
extension UIViewController {
func locationPermissionManager(with locationAuthStatus: CLAuthorizationStatus) {
switch locationAuthStatus {
case .notDetermined:
CLLocationManager().requestAlwaysAuthorization()
case .denied, .restricted:
Amplitude.instance()?.logEvent("Location Service: either denied or restricted")
showLocationAceessDeniedAlert()
case .authorizedAlways:
Amplitude.instance()?.logEvent("Location Service: Authorized Always")
case .authorizedWhenInUse:
Amplitude.instance()?.logEvent("Location Service: Authorized when in use")
}
}
func showLocationAceessDeniedAlert() {
let alertController = UIAlertController(title: "Permission update", message: "📍Location service needs to be enabled from settings.", preferredStyle: .alert)
let settingAction = UIAlertAction(title: "Open Settings", style: .default) { alertAction in
if let appSettings = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(appSettings, options: [:], completionHandler: nil)
}
}
alertController.addAction(settingAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
}
关于swift - 扩展与 LocationPermission 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53638690/