首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CLLocationManagerDelegate Singleton

CLLocationManagerDelegate Singleton
EN

Stack Overflow用户
提问于 2017-12-31 09:44:29
回答 2查看 3.3K关注 0票数 4

我正在尝试使用这个接口创建一个CLLocationManager Singleton,这个接口只在ViewController上使用Location.start(),但是我没有看到任何具有这种结构的位置更新。

我正在使用print(mostRecentLocation)来检测位置更新。

我怎么才能解决这个问题?

这是我在Location.swift中的代码

代码语言:javascript
复制
import CoreLocation

class Location: NSObject, CLLocationManagerDelegate {
    private static var locationManager: CLLocationManager = {
        var manager = CLLocationManager()
        manager.desiredAccuracy = kCLLocationAccuracyBest
        return manager
    }()

    static func start() {
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let mostRecentLocation = locations.last else {
          return
        }

        print(mostRecentLocation)
    }
}

以及在ViewController.swift

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()
    Location.start()
}

我看到这个https://github.com/igroomgrim/CLLocationManager-Singleton-in-Swift

而这个https://github.com/irfanlone/CLLocationManager-Singleton-Swift

但我不喜欢这个界面。

EN

回答 2

Stack Overflow用户

发布于 2017-12-31 10:02:26

在某个地方,您必须设置位置管理器的委托。

无论如何,单例应该是类的(共享)实例,所以我建议如下所示

代码语言:javascript
复制
class Radar: NSObject, CLLocationManagerDelegate {

    static let shared = Radar()

    let locationManager : CLLocationManager

    override init() {
        locationManager = CLLocationManager()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        super.init()
        locationManager.delegate = self
    }

    func start() {
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let mostRecentLocation = locations.last else {
            return
        }

        print(mostRecentLocation)
    }

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

然后叫它

代码语言:javascript
复制
Radar.shared.start()

若要获得所需的语法,请使用类包装器和类方法。

代码语言:javascript
复制
class Location {

    static func start() {
        Radar.shared.start()
    }
}

然后你可以写

代码语言:javascript
复制
Location.start()
票数 10
EN

Stack Overflow用户

发布于 2019-01-06 09:55:07

  1. 创建一个位置管理器单音FLocationManager.swift,并复制粘贴下面的片段,在快速4.1:-
代码语言:javascript
复制
import Foundation
import CoreLocation


class FLocationManager: NSObject, CLLocationManagerDelegate {

    static let shared = FLocationManager()
    let locationManager : CLLocationManager
    var locationInfoCallBack: ((_ info:LocationInformation)->())!

    override init() {
        locationManager = CLLocationManager()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = kCLLocationAccuracyHundredMeters
        super.init()
        locationManager.delegate = self
    }

    func start(locationInfoCallBack:@escaping ((_ info:LocationInformation)->())) {
        self.locationInfoCallBack = locationInfoCallBack
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }

    func stop() {
        locationManager.stopUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let mostRecentLocation = locations.last else {
            return
        }
        print(mostRecentLocation)
        let info = LocationInformation()
        info.latitude = mostRecentLocation.coordinate.latitude
        info.longitude = mostRecentLocation.coordinate.longitude


        //now fill address as well for complete information through lat long ..
        let geocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(mostRecentLocation) { (placemarks, error) in
            guard let placemarks = placemarks, let placemark = placemarks.first else { return }
            if let city = placemark.locality,
                let state = placemark.administrativeArea,
                let zip = placemark.postalCode,
                let locationName = placemark.name,
                let thoroughfare = placemark.thoroughfare,
                let country = placemark.country {
                info.city     = city
                info.state    = state
                info.zip = zip
                info.address =  locationName + ", " + (thoroughfare as String)
                info.country  = country
            }
            self.locationInfoCallBack(info)
        }
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error)
        locationManager.stopUpdatingLocation()
    }
}

class LocationInformation {
    var city:String?
    var address:String?
    var latitude:CLLocationDegrees?
    var longitude:CLLocationDegrees?
    var zip:String?
    var state :String?
    var country:String?
    init(city:String? = "",address:String? = "",latitude:CLLocationDegrees? = Double(0.0),longitude:CLLocationDegrees? = Double(0.0),zip:String? = "",state:String? = "",country:String? = "") {
        self.city    = city
        self.address = address
        self.latitude = latitude
        self.longitude = longitude
        self.zip        = zip
        self.state = state
        self.country = country
    }
}

  1. 在应用程序Info.plist中添加权限请求(右击并选择打开选项为“源代码”):-

代码语言:javascript
复制
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>

3.在您的任意UIViewController中使用单音位置管理器,并在任何您想要的地方停止。

代码语言:javascript
复制
  override func viewDidLoad() {
        super.viewDidLoad()
        //use below code snippet for your single tone location manager class in entire app where ever you want 
        FLocationManager.shared.start { (info) in
            print(info.longitude ?? 0.0)
            print(info.latitude ?? 0.0)
            print(info.address ?? "")
            print(info.city ?? "")
            print(info.zip ?? "")
        }

    }

  1. 对于应用程序中的停止位置管理器服务,您必须使用以下命令:-
代码语言:javascript
复制
   FLocationManager.shared.stop()

干杯!快乐编码:)

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48041459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档