无法调用locationManager_ :: didUpdateLocations :)

问题描述

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

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

UserLocation.swift

import io
import re

data = io.StringIO("""
    function foo();             
    function maybeanotherfoo(); 
    int maybemoregarbage;       

    
    product_serial = "CDE1102"; 
    unnecessary_info1 = 10;     
    unnecessary_info2 = "red"   
    product_id = 1134412;       
    unnecessary_info3 = "88"    

    product_serial = "DD1232";  
    product_id = 3345111;       
    unnecessary_info1 = "22"    
    unnecessary_info2 = "panda" 

    product_serial = "CDE1102"; 
    unnecessary_info1 = 10;     
    unnecessary_info2 = "red"   
    unnecessary_info3 = "bear"  
    unnecessary_info4 = 119     
    product_id = 1112331;       
    unnecessary_info5 = "jj"    
""")

objects = []

current_object = {}
for line in data:
    line = line.strip()  # Remove leading and trailing whitespace
    m = re.match(r'^(product_id|product_serial)\s*=\s*(\d+|"(?:.+?)");?$',line)

    if m:
        key,value = m.groups()
        current_object[key] = value.strip('"')
        if len(current_object) == 2:  # Got the two keys we want,ship the object
            objects.append(current_object)
            current_object = {}

print(objects)

然后,我在 AppDelegate.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 } } 调用函数,如下所示:

application(_::didFinishLaunchingWithOptions:)

但是,仅成功调用func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let getUserLocation = UserLocation() getUserLocation.locationSetup() } 函数,从未调用相关函数locationManager(_ :: didUpdateLocations :)。我将locationSetup()的第一行放在了print("locationManager getting start")中,从未打印出来

顺便说一句,在info.plist中,我已经设置了“隐私权-使用时的位置,用法说明”。

有人可以帮我吗?

解决方法

您的getUserLocation是局部变量。创建它后1毫秒,它就不存在了。因此,从来没有时间任何事情(例如位置更新)。将其提升为实例变量,使其寿命更长:

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

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

,

请交叉检查模拟器的位置:模拟器->调试->位置,在您的情况下不应为无...

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...