我有一个全局共享类,我用来从应用程序中的任何地方提供某些数据。我一直在尝试实现的一件事是跟踪用户位置并将这些坐标保存在此全局共享类中。
我尝试实现我在旧类中工作的所有必要组件,但是在这样做时,我被迫添加一些协议存根,我不确定该怎么做。
此外,我在行上有一个错误func
自我() -> Self {
这是代码:
extension GlobalSharedData: CLLocationManagerDelegate {
func isEqual(_ object: Any?) -> Bool {
<#code#>
}
var hash: Int {
<#code#>
}
var superclass: AnyClass? {
<#code#>
}
func `self`() -> Self {
<#code#>
}
func perform(_ aSelector: Selector!) -> Unmanaged<AnyObject>! {
<#code#>
}
func perform(_ aSelector: Selector!, with object: Any!) -> Unmanaged<AnyObject>! {
<#code#>
}
func perform(_ aSelector: Selector!, with object1: Any!, with object2: Any!) -> Unmanaged<AnyObject>! {
<#code#>
}
func isProxy() -> Bool {
<#code#>
}
func isKind(of aClass: AnyClass) -> Bool {
<#code#>
}
func isMember(of aClass: AnyClass) -> Bool {
<#code#>
}
func conforms(to aProtocol: Protocol) -> Bool {
<#code#>
}
func responds(to aSelector: Selector!) -> Bool {
<#code#>
}
var description: String {
<#code#>
}
可以做到吗?如果是这样?
看来您错过了子分类NSObject
import Foundation
import CoreLocation
protocol LocationServiceDelegate {
func tracingLocation(currentLocation: CLLocation)
func tracingLocationDidFailWithError(error: NSError)
}
class LocationSingleton: NSObject,CLLocationManagerDelegate {
var locationManager: CLLocationManager?
var lastLocation: CLLocation?
var delegate: LocationServiceDelegate?
static let shared:LocationSingleton = {
let instance = LocationSingleton()
return instance
}()
override init() {
super.init()
self.locationManager = CLLocationManager()
guard let locationManagers=self.locationManager else {
return
}
if CLLocationManager.authorizationStatus() == .notDetermined {
locationManagers.requestAlwaysAuthorization()
locationManagers.requestWhenInUseAuthorization()
}
if #available(iOS 9.0, *) {
// locationManagers.allowsBackgroundLocationUpdates = true
} else {
// Fallback on earlier versions
}
locationManagers.desiredAccuracy = kCLLocationAccuracyBest
locationManagers.pausesLocationUpdatesAutomatically = false
locationManagers.distanceFilter = 0.1
locationManagers.delegate = self
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else {
return
}
self.lastLocation = location
updateLocation(currentLocation: location)
}
@nonobjc func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
locationManager?.requestAlwaysAuthorization()
break
case .authorizedWhenInUse:
locationManager?.startUpdatingLocation()
break
case .authorizedAlways:
locationManager?.startUpdatingLocation()
break
case .restricted:
// restricted by e.g. parental controls. User can't enable Location Services
break
case .denied:
// user denied your app access to Location Services, but can grant access from Settings.app
break
default:
break
}
}
// Private function
private func updateLocation(currentLocation: CLLocation){
guard let delegate = self.delegate else {
return
}
delegate.tracingLocation(currentLocation: currentLocation)
}
private func updateLocationDidFailWithError(error: NSError) {
guard let delegate = self.delegate else {
return
}
delegate.tracingLocationDidFailWithError(error: error)
}
func startUpdatingLocation() {
print("Starting Location Updates")
self.locationManager?.startUpdatingLocation()
// self.locationManager?.startMonitoringSignificantLocationChanges()
}
func stopUpdatingLocation() {
print("Stop Location Updates")
self.locationManager?.stopUpdatingLocation()
}
func startMonitoringSignificantLocationChanges() {
self.locationManager?.startMonitoringSignificantLocationChanges()
}
// #MARK: get the alarm time from date and time
}
使用
LocationSingleton.shared.delegate = self //optional inside the vc
LocationSingleton.shared.startUpdatingLocation() // start this as earlier as possible so when you check of lastLocation is could have a value