CLLocationManagerDelegate Singleton

时间:2017-12-31 09:44:30

标签: ios swift

我尝试在CLLocationManager上仅使用此界面Location.start()创建ViewController单身人士,但我看不到此结构的任何位置更新。print(mostRecentLocation)来检测位置更新。
我该如何解决这个问题?

以下是Location.swift中的代码:

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

override func viewDidLoad() {
    super.viewDidLoad()
    Location.start()
}

我看到这个https://github.com/igroomgrim/CLLocationManager-Singleton-in-Swift
和这个https://github.com/irfanlone/CLLocationManager-Singleton-Swift了,但我不喜欢这个界面。

2 个答案:

答案 0 :(得分:8)

您必须在某处设置位置管理员的代理。

无论如何,单身人士应该是一个类的(共享)实例,所以我推荐这样的东西

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()
    }
}

并称之为

Radar.shared.start()

要获得所需的语法,请使用带有类方法的类包装器

class Location {

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

然后你可以写

Location.start()

答案 1 :(得分:2)

  
      
  1. 创建一个位置管理器单音 FLocationManager.swift 并在swift 4.1中的以下代码段下复制粘贴:-
  2.   
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(右键单击并选择打开为“源代码”的选项)文件中添加权限请求:-
  2.   

enter image description here

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
  

3。在任何UIViewController中使用Single Tone位置管理器,然后在所需的任何位置停止。

  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. 要在您的应用中停止位置管理器服务,您必须使用此功能:-
  2.   
   FLocationManager.shared.stop()

干杯!快乐编码:)