位置权限检查和授权 [英] Location permission check and authorization

查看:122
本文介绍了位置权限检查和授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的程序在地图上显示用户的位置.它起初工作,然后随机停止,所以我在下面添加了代码以尝试对其进行修复,但是我遇到了问题.提前致谢!

I want my program to display the users location on the map. It was working at first then randomly stopped so i added the code below to try to fix it but I'm having problems. Thanks in advance!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        if CLLocationManager.locationServicesEnabled()
        {

            let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
            if status == CLAuthorizationStatus.NotDetermined
            {
                locationManager.requestAlwaysAuthorization()
            }
            } else {

                print("locationServices disenabled")
            }
            locationManager.startUpdatingLocation()
            self.locationManager.startUpdatingLocation()
            self.mapView.showsUserLocation = true
            mapView.delegate = self
            centerMapOnLocation(initialLocation)
            addBoundry()
    }

推荐答案

您需要致电

locationManager.requestAlwaysAuthorization() 

也是

 locationManager.requestWhenInUseAuthorization()

也可以在前景中顺利使用位置!!!

to use location smoothly in foreground also!!!

,并且您不需要两个实例来调用startupdatinglocation.保持一个.您应该使用实例变量或全局变量而不是局部变量来获取整个作用域中的位置.

and you should not need two instance to call startupdatinglocation. keep one. you should use instance or global variable instead of local to get location throughout the scope.

更新:

您必须在info.plist中设置两个键,例如NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription及其使用说明.

You have to set two keys in info.plist like, NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription with it's usage description.

这篇关于位置权限检查和授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆