swift - 扩展与 LocationPermission 类

标签 swift

我一直在开发我的位置感知应用程序。 我正在尝试从 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
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/

相关文章:

swift - AVPlayerViewController 适用于 iOS8 但不适用于 IOS7

swift - SpriteKit 场景在模态呈现 swift 后变形

swift - 如何添加UILongPressGestureRecognizer释放功能

ios - 如何在swift中使用后台线程?

ios - 数组 Swift 的相似度百分比

swift - 带有单例类 swift 的异步完成 block

ios - 如何从 tableview 单元格中收集多个动态值并存储在数组中

xcode - 我将如何以编程方式快速引用我的应用程序中的主 Storyboard?

swift - CMMotionManager 倾斜以左右移动节点 Swift 4

ios - spriteKit 碰撞不起作用