【问题标题】:Can not called locationManager(_::didUpdateLocations:)不能调用 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 。

谁能帮帮我?

【问题讨论】:

    标签: ios swift cllocationmanager


    【解决方案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...

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-30
        • 2013-11-06
        • 2015-12-12
        • 2014-11-28
        • 1970-01-01
        相关资源
        最近更新 更多