gpt4 book ai didi

swift - 在模拟器中启用 CoreLocation

转载 作者:行者123 更新时间:2023-11-28 14:32:59 26 4
gpt4 key购买 nike

didUpdateLocations 没有触发- CLLocationManagerDelegate 已使用 View Controller 作为委托(delegate)正确实现

import Foundation
import UIKit
import CoreLocation

protocol LocationServiceDelegate {
func tracingLocation(currentLocation: CLLocation)
func tracingLocationDidFailWithError(error: NSError)
}

class LocationService: NSObject, CLLocationManagerDelegate {

static var sharedInstance = LocationService()

var locationManager: CLLocationManager?
var currentLocation: CLLocation?
var delegate: LocationServiceDelegate?
var paymentVC: PaymentViewController?

override init() {
super.init()

self.locationManager = CLLocationManager()
guard let locationManager = self.locationManager else {
return
}

if CLLocationManager.authorizationStatus() == .notDetermined {
locationManager.requestAlwaysAuthorization()
}

locationManager.distanceFilter = 200

}

func startUpdatingLocation() {
print("Starting Location Updates")
self.locationManager!.startUpdatingLocation()
}

func stopUpdatingLocation() {
print("Stop Location Updates")
self.locationManager?.stopUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

guard let location = locations.last else {
return
}

self.currentLocation = location
updateLocation(currentLocation: location)
}

private func locationManager(manager: CLLocationManager, didFailWithError error: Error) {
updateLocationDidFailWithError(error: error as NSError)
}

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)
}
}
  • 这是 View Controller 的扩展,我在其中实现了用于核心定位跟踪的自定义协议(protocol)
  • 我在 viewDidLoad 中调用 startUpdatingLocations()

    extension PaymentViewController: LocationServiceDelegate,CLLocationManagerDelegate {

    func tracingLocation(currentLocation: CLLocation) {
    locationService.currentLocation = currentLocation
    }

    func tracingLocationDidFailWithError(error: NSError) {
    print("Error message: \(error.localizedDescription)")
    }

    func startUpdatingLocations() {
    locationService.locationManager?.delegate = self
    locationService.delegate = self
    locationService.startUpdatingLocation()
    }

    func stopUpdatingLocations() {
    LocationService.sharedInstance.stopUpdatingLocation()
    }
    }

corelocation 跟踪未在模拟器中触发。但是,这是启用的。

enter image description here不幸的是,我现在无法使用设备进行测试

最佳答案

我知道在模拟器上模拟核心位置跟踪的唯一方法是选择模拟器调试选项卡中可用的位置选项之一。

enter image description here

关于swift - 在模拟器中启用 CoreLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51115828/

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