操作无法完成-位置

时间:2019-12-07 20:24:43

标签: ios swift location

我正在尝试获取用户的当前位置,但失败并出现下一个错误:

  

--- LocationManager失败了---操作无法完成。 (kCLErrorDomain错误0。)

我真的不知道我在做什么错,或者是否与我在模拟器中运行应用程序有关(在启动时,我从未看到模拟器模拟位置)

import UIKit
import CoreLocation

class CardStackViewController: UIViewController, CLLocationManagerDelegate, NetworkManagerDelegate {

    private var networkManager: NetworkManager = NetworkManager()
    private var games: [Game] = []
    private var parties: [Party] = []
    private var user: User? = nil
    private let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        networkManager.delegate = self
        networkManager.getGames()
        networkManager.getUser(email: "xyz@hotmail.com")
    }

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

    //MARK: - Location

    func getCurrentLocation() {
        locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
        // networkManager.getPartiesNearYou(maxDistance: 10, userId: "5db8838eaffe445c66076a89", latitude: locValue.latitude, longitude: locValue.longitude)
        // mock request
        networkManager.getPartiesNearYou(maxDistance: 10, userId: "5db8838eaffe445c66076a89", latitude: 50.92, longitude: 4.06)
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("---LocationManager did fail---", error.localizedDescription)
    }

    // MARK: - NetworkDelegate
    func updateGames(_ networkManager: NetworkManager, _ games: [Game]) {
        self.games = games
    }

    func updateParties(_ networkManager: NetworkManager, _ parties: [Party]) {
        self.parties = parties
    }

    func updateUser(_ networkManager: NetworkManager, _ user: User) {
        self.user = user
    }

    func didFail(with error: Error) {
        if let animation = Animation.named("error", subdirectory: "Animations") {
            animationView.animation = animation
            animationView.loopMode = .loop
            view.addSubview(animationView)
            animationView.play()
        }
        print("---ERROR---", error.localizedDescription)
    }
}

0 个答案:

没有答案