加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 我的基于CoreLocation的Swift应用程序不会要求用户访问位

发布时间:2020-12-14 17:44:12 所属栏目:百科 来源:网络整理
导读:我正在构建一个基于CoreLocation的应用程序,该应用程序根据纬度,经度,水平精度,海拔高度,垂直精度,行进距离等6个参数向用户显示其位置. 它假设要求用户允许第一次访问位置,但我也尝试重置所有模拟器的设置. 灰色部分将在稍后填充地图. 这就是我的View.Contro
我正在构建一个基于CoreLocation的应用程序,该应用程序根据纬度,经度,水平精度,海拔高度,垂直精度,行进距离等6个参数向用户显示其位置.

它假设要求用户允许第一次访问位置,但我也尝试重置所有模拟器的设置.

This is how my Main.storyboard look like

灰色部分将在稍后填充地图.

这就是我的View.Controller.swift的样子:

//  Created by 16246 on 6/7/16.
//  Copyright ? 2016 16246. All rights reserved.
//

import UIKit
import CoreLocation

class ViewController: UIViewController,CLLocationManagerDelegate {
    private let LocationManager = CLLocationManager()
    private var previousPoint:CLLocation?
    private var totalMovementDistance:CLLocationDistance = 0

    @IBOutlet var latitudeLabel: UILabel!
    @IBOutlet var longitudeLabel: UILabel!
    @IBOutlet var horizontalAccuracy: UILabel!
    @IBOutlet var altitudeLabel: UILabel!
    @IBOutlet var verticalAccuracyLabel: UILabel!
    @IBOutlet var distanceTraveledLabel: UILabel!



    override func viewDidLoad() {
        super.viewDidLoad()
        LocationManager.delegate = self
        LocationManager.desiredAccuracy = kCLLocationAccuracyBest
        LocationManager.requestAlwaysAuthorization()


        // Do any additional setup after loading the view,typically from a nib.
    }

    func locationManager(manager: CLLocationManager,didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        print("Authorization Status Changed to (status.rawValue)")
        switch status {
        case .Authorized,.AuthorizedWhenInUse:
            LocationManager.startUpdatingLocation()
        default:
            LocationManager.stopUpdatingLocation()
        }
    }

    func locationManager(manager: CLLocationManager,didFailWithError error: NSError) {
        let errorType = error.code == CLError.Denied.rawValue ? "Access Denied": "Error (error.code)"
        let alertController = UIAlertController(title: "Location Manager Error",message: errorType,preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "OK",style: .Cancel,handler: {action in})
        alertController.addAction(okAction)
        presentViewController(alertController,animated: true,completion: nil)
    }

    func locationManager(manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
        let newLocation = (locations as [CLLocation]) [locations.count-1]

        let latitudeString = String(format: "%gu{00B0}",newLocation.coordinate.latitude)
        latitudeLabel.text = latitudeString

        let longitudeString = String(format: "%gu{00B0}",newLocation.coordinate.longitude)
        longitudeLabel.text = longitudeString

        let horizontalAccuracyString = String(format: "%gu{00B0}",newLocation.horizontalAccuracy)
        horizontalAccuracy.text = horizontalAccuracyString

        let altitudeString = String(format: "%gu{00B0}",newLocation.altitude)
        altitudeLabel.text = altitudeString

        let verticalAccuracyString = String(format: "%gu{00B0}",newLocation.verticalAccuracy)
        verticalAccuracyLabel.text = verticalAccuracyString

        if newLocation.horizontalAccuracy < 0 {
            return
        }

        if newLocation.horizontalAccuracy > 100 ||
            newLocation.verticalAccuracy > 50 {
                return
        }

        if previousPoint == nil {
            totalMovementDistance = 0
        } else {
            totalMovementDistance += newLocation.distanceFromLocation(previousPoint!)
        }

        previousPoint = newLocation

        let distanceString = String(format: "%gm",totalMovementDistance)
        distanceTraveledLabel.text = distanceString

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

这是我的Info.plist文件:

enter image description here

这是我的模拟器:

enter image description here

我想要这样的输出:

enter image description here

自从过去2天以来我一直坚持这一点.品脱啤酒给一个帮我相处的极客.

解决方法

将LocationManager.requestAlwaysAuthorization()移动到viewDidAppear方法.

编辑:

好的,您正在询问requestAlwaysAuthorization,但在info.plist中设置了When in usage …键入条目,因此将requestAlwaysAuthorization更改为requestWhenInUseAuthorization

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读