ios – didUpdateLocations从未调用过

ios – didUpdateLocations从未调用过,第1张

概述我正在尝试获取用户的位置.为此,我在info.plist中设置了以下属性: 我还在viewDidLoad方法中添加了以下代码以及下面的函数.问题是locationManager(manager,didUpdate ….)函数永远不会被调用,我也从未被提示访问位置的权限,即使我已经删除并再次安装了应用程序.我在iPad上测试,而不是在模拟器上测试. didFailWithError函数永远不会被调用 我正在尝试获取用户的位置.为此,我在info.pList中设置了以下属性:

我还在vIEwDIDLoad方法中添加了以下代码以及下面的函数.问题是locationManager(manager,dIDUpdate ….)函数永远不会被调用,我也从未被提示访问位置的权限,即使我已经删除并再次安装了应用程序.我在iPad上测试,而不是在模拟器上测试. dIDFailWithError函数永远不会被调用.

self.locationManager.delegate = self    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest    self.locationManager.requestWhenInUseAuthorization()    self.locationManager.startUpdatingLocation()func locationManager(manager: CLLocationManager,dIDUpdateLocations locations: [CLLocation]) {    print("UPDATING")    let locValue:CLLocationCoordinate2D = manager.location!.coordinate    let latitude = locValue.latitude    let longitude = locValue.longitude    latitudeText = "\(latitude)"    longitudeText = "\(longitude)"    if let a = latitudeText,b = longitudeText {        print(a)        print(b)        self.locationManager.stopUpdatingLocation()        if (userAlreadyExist()) {            dispatch_async(dispatch_get_main_queue(),{                //self.performSegueWithIDentifIEr("segueWhenLoggedIn",sender: self)                self.performSegueWithIDentifIEr("showCamera",sender: self)                // self.performSegueWithIDentifIEr("showTabbarController",sender: self)            })        }        else {            dispatch_async(dispatch_get_main_queue(),{                self.performSegueWithIDentifIEr("segueWhenLoggedOut",sender: self)            })        }    }}func locationManager(manager: CLLocationManager,dIDFailWithError error: NSError) {    print(error.localizedDescription)}

编辑:

我添加了以下代码片段:

if CLLocationManager.locationServicesEnabled() {        print("yes")    }    else {        print("no")    }

它返回是.我还检查了我的设备,locationServices已启用,应用程序已列在那里,但是所有其他应用程序都在其旁边写着“While Using”,“Never”或“Always”,我没有写任何东西.

解决方法 你在哪里开始更新位置?例如:
//location manager    lazy var locationManager: CLLocationManager = {        var _locationManager = CLLocationManager()        _locationManager.delegate = self        _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters        _locationManager.activityType = .automotiveNavigation        _locationManager.distanceFilter = 10.0  // Movement threshold for new events        _locationManager.allowsBackgroundLocationUpdates = true // allow in background        return _locationManager    }()    if CLLocationManager.locationServicesEnabled() {                locationManager.startUpdatingLocation() // start location manager            }

这是一个工作的控制器代码:
设置自定义iOS目标属性也很重要.

将这两行添加到Info.pList:

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

////  VIEwController.swift//  LocationTest2import UIKitimport CoreLocationclass VIEwController: UIVIEwController {    //location manager    lazy var locationManager: CLLocationManager = {        var _locationManager = CLLocationManager()        _locationManager.delegate = self        _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters        _locationManager.activityType = .automotiveNavigation        _locationManager.distanceFilter = 10.0  // Movement threshold for new events      //  _locationManager.allowsBackgroundLocationUpdates = true // allow in background        return _locationManager    }()    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()    }    overrIDe func vIEwWillAppear(animated: Bool) {        super.vIEwWillAppear(animated)        //allow location use        locationManager.requestAlwaysAuthorization()        print("dID load")        print(locationManager)        //get current user location for startup       // if CLLocationManager.locationServicesEnabled() {            locationManager.startUpdatingLocation()       // }    }}// MARK: - CLLocationManagerDelegateextension VIEwController: CLLocationManagerDelegate {    func locationManager(manager: CLLocationManager,dIDUpdateLocations locations: [CLLocation]) {        for location in locations {            print("**********************")            print("Long \(location.coordinate.longitude)")            print("Lati \(location.coordinate.latitude)")            print("Alt \(location.altitude)")            print("Sped \(location.speed)")            print("Accu \(location.horizontalAccuracy)")            print("**********************")        }    }}
总结

以上是内存溢出为你收集整理的ios – didUpdateLocations从未调用过全部内容,希望文章能够帮你解决ios – didUpdateLocations从未调用过所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1100061.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存