gpt4 book ai didi

ios - 您可以在共享的全局类中跟踪用户的位置吗?

转载 作者:行者123 更新时间:2023-11-29 05:46:22 25 4
gpt4 key购买 nike

我有一个全局共享类,我用它来从我的应用程序中的任何位置提供某些数据。我一直试图实现的一件事是跟踪用户位置并将这些坐标保存在这个全局共享类中。

我尝试实现所有必要的组件,这些组件是我在旧类中工作的,但这样做时我被迫添加一些协议(protocol) stub ,我不知道该怎么办。

此外,我在 funcself() -> 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

关于ios - 您可以在共享的全局类中跟踪用户的位置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56121213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com