【问题标题】:locationManager.location returns nillocationManager.location 返回 nil
【发布时间】:2019-06-20 17:26:12
【问题描述】:

我正在使用swift4.2 和Xcode 10,我正在尝试使iOS 应用程序使用定位服务,但它给了我例外:
Fatal error: Unexpectedly found nil while unwrapping an Optional value

所以我尝试检查 location 是否返回 nil 所以我复制我的代码并打印位置并返回 null ,我从 product>scheme 模拟了 Xcode 中的位置strong>>编辑方案>默认位置并检查调试区域中的位置,它模拟到我选择的位置有人知道问题吗?

import CoreLocation

class LocationVC: UIViewController,CLLocationManagerDelegate {

    let locationManager = CLLocationManager()
    var currentlocation:CLLocation!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startMonitoringSignificantLocationChanges()
   }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        authorizelocationstates()
    }

    func authorizelocationstates(){
        if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
            currentlocation = locationManager.location
            print(currentlocation)
        }
        else{
            locationManager.requestWhenInUseAuthorization()
            authorizelocationstates()
        }
        }

【问题讨论】:

标签: ios swift core-location cllocationmanager


【解决方案1】:

我已经运行了你的代码,我刚刚在info.plist 文件中添加了缺少的密钥隐私 - 使用时的位置使用说明。

我已经对您的代码进行了一些更改并获得了nil 值,因为方法被多次调用并且当用户授予权限和方法再次被调用并要打印位置详细信息但事实仍然是locationManager 变量没有用户位置数据。

当委托didUpdateLocations 调用时,在locationManager 中获取位置详细信息

我对你的代码做了一些改动:

import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
    var locationManager = CLLocationManager()
    var currentlocation:CLLocation!



    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
     //   authorizelocationstates()
    }

    func authorizelocationstates(){
        if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
            currentlocation = locationManager.location
            print(currentlocation)
        }
        else{
            // Note : This function is overlap permission
            //  locationManager.requestWhenInUseAuthorization()
           //  authorizelocationstates()
        }
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        locationManager = manager
        // Only called when variable have location data
        authorizelocationstates()
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        // Get Location Permission one time only
        locationManager.requestWhenInUseAuthorization()
        // Need to update location and get location data in locationManager object with delegate
        locationManager.startUpdatingLocation()
        locationManager.startMonitoringSignificantLocationChanges()

    }

}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 2017-12-16
    • 2014-09-27
    • 2017-06-11
    • 2014-12-29
    • 2019-03-14
    • 2016-06-16
    相关资源
    最近更新 更多