不能调用 locationManager(_::didUpdateLocations:)

Posted

技术标签:

【中文标题】不能调用 locationManager(_::didUpdateLocations:)【英文标题】:Can not called locationManager(_::didUpdateLocations:) 【发布时间】:2020-09-08 17:54:08 【问题描述】:

我尝试在用户启动应用时获取坐标。

我在独立文件中设置了 locationManager 代码:

UserLocation.swift

import Foundation
import CoreLocation

class UserLocation: NSObject, CLLocationManagerDelegate 
    var userCurrentLocation: CLLocationCoordinate2D? 
    let locationManager = CLLocationManager()
    
    func locationSetup() 
        locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.authorizationStatus() != CLAuthorizationStatus.authorizedWhenInUse 
            print("authorization error")
            return
        
        locationManager.distanceFilter = 300
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.delegate = self
        locationManager.startUpdatingLocation()
        print("startUpdatingLocation")
        if CLLocationManager.locationServicesEnabled() 
            print("locationServicesEnabled")
        
    
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
        print("locationManager getting start")
        if let location = locations.last 
            self.userCurrentLocation = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
            print("print location in locationManager: \(String(describing: userCurrentLocation?.latitude))")
            locationManager.stopUpdatingLocation()
            
        
    
    func locationManager(_ manager: CLLocationManager, didFinishDeferredUpdatesWithError error: Error?) 
        print(error?.localizedDescription as Any)
        return
    
    


然后,我像这样调用 AppDelegate.swiftapplication(_::didFinishLaunchingWithOptions:) 中的函数:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 
        let getUserLocation = UserLocation()
        getUserLocation.locationSetup()

但是,只有成功调用locationSetup()函数,从未调用过相关函数locationManager(_::didUpdateLocations:)。 print("locationManager getting start") 我把第一行放在locationManager(_::didUpdateLocations:) 里,从来没有打印出来

顺便说一句,在 info.plist 中,我已经设置了 Privacy - Location When In Use Usage Description 。

谁能帮帮我?

【问题讨论】:

【参考方案1】:

您的getUserLocation 是一个局部变量。它在您创建后 1 毫秒就消失了。所以它永远没有时间任何事情(例如位置更新)。将其提升为实例变量,使其寿命更长:

var getUserLocation : UserLocation?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 
    self.getUserLocation = UserLocation()
    self.getUserLocation?.locationSetup()
    return true

(也请使用更好的变量名。对象引用不应以动词开头。)

【讨论】:

【参考方案2】:

请交叉检查模拟器位置:模拟器 -> 调试 -> 位置在你的情况下不应该是 None...

希望这会对您有所帮助...谢谢:)

【讨论】:

以上是关于不能调用 locationManager(_::didUpdateLocations:)的主要内容,如果未能解决你的问题,请参考以下文章

locationManager(_:​did​Change​Authorization:​) 在应用首次运行时执行?

为啥 LocationManager 会多次调用 startUpdatingLocation?

LocationManager.getLastKnownLocation() 返回 null,并且永远不会调用 onLocationChanged

locationManager:didFailWithError: 如果用户位置服务关闭,则不调用

快速组合中未调用 sink()

locationmanager didenterregion 没有被调用