我一直在研究位置感知应用程序。 我试图从视图控制器中提取位置权限流。这样我就有了更干净的代码。
我编写了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)
}
}
我既能正常工作,又能按预期工作。我现在很困惑,这是一个更好的解决方案。
我想在这里问聪明的人。根据编码标准,哪种解决方案是更好的解决方案?
答案 0 :(得分:1)
如果代码与目标类或结构广泛相关,则扩展名很有用。
看看代码,它与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)
}
}