ios - MVP中的核心位置

标签 ios swift core-location mvp

在我的项目中,我有一个符合 CLLocationManagerDelegate 协议(protocol)的 LocationService 类,用于检测当前用户的位置。

class LocationService: NSObject, CLLocationManagerDelegate {

    fileprivate let locationManager = CLLocationManager()
    var location: Location?  // Location(lat: Double, lon: Double)

    override init() {
        super.init()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

    }

    func getCurrentLocation() -> Location? {
        locationManager.startUpdatingLocation()
         // how can I catch a location?
        return location
    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.last {
            if location.horizontalAccuracy > 0 {
                locationManager.stopUpdatingLocation()
                self.location = Location(lat: location.coordinate.latitude, lon: location.coordinate.latitude)
            }
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }

}

我希望我的 WeatherPresenter 触发 LocationService 中的位置更新,并在找到位置后立即获取结果。有什么办法吗?

class WeatherPresenter {

    unowned let delegate: WeatherViewDelegate
    let weatherService = WeatherService()
    let locationService = LocationService()

    init(with delegate: WeatherViewDelegate) {
        self.delegate = delegate
    }

    func getWeatherForCurrentLocation() {
        if let location = locationService.getCurrentLocation() {
            //...
        }
    }
}

最佳答案

您可以使用 Delegate 通知 WeatherPresenter 来自 LocationService 的更改

protocol LocationServiceDelegate: class { // Delegate protocol
    func didUpdateLocation()
}

class LocationService: NSObject, CLLocationManagerDelegate {

    weak var delegate: LocationServiceDelegate?

    fileprivate let locationManager = CLLocationManager()
    var location: Location?  // Location(lat: Double, lon: Double)

    override init() {
        super.init()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

    }

    func startUpdatingLocation() { // Start updating called from presenter
        locationManager.startUpdatingLocation()
    }

    func getCurrentLocation() -> Location? {
        // how can I catch a location?
        return location
    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.last {
            if location.horizontalAccuracy > 0 {
                locationManager.stopUpdatingLocation()
                self.location = Location(lat: location.coordinate.latitude, lon: location.coordinate.latitude)
                self.delegate?.didUpdateLocation() // Notify delegate on change
            }
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }

}


class WeatherPresenter: LocationServiceDelegate {
    unowned let delegate: WeatherViewDelegate
    let weatherService = WeatherService()
    let locationService = LocationService()

    init(with delegate: WeatherViewDelegate) {
        self.delegate = delegate
        self.locationService.delegate = self // Set self as delegate
        self.locationService.startUpdatingLocation()  // Requests start updating location
    }

    func didUpdateLocation() { // This will be called on location change
        self.getWeatherForCurrentLocation()
    }

    func getWeatherForCurrentLocation() {
        if let location = locationService.getCurrentLocation() {
            //...
        }
    }
}

关于ios - MVP中的核心位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55290217/

相关文章:

ios - 我的应用程序中的 cocoapods 框架比示例中慢得多

ios - 无论设备如何,以编程方式从屏幕底部对齐标签 - iOS

ios - 访问 UIPageViewController 手势识别器以启用滑动删除

ios - 下载数据后 TableView 未更新

ios - 无法从 CLGeocoder reverseGeocodeLocation 返回字符串

ios - 多个 tableView reloadData()

ios - 在 Swift 中创建纯色的 UIImage

ios - 聆听音量按钮按正确方式(iOS 15)

iOS 8 CLLocationManager

ios - 即使设置了更高的 gps 精度 (kCLLocationAccuracyThreeKilometers),位置也会更新