Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取有关请求位置权限的错误_Ios_Swift_Permissions_Location_Mapkit - Fatal编程技术网

Ios 获取有关请求位置权限的错误

Ios 获取有关请求位置权限的错误,ios,swift,permissions,location,mapkit,Ios,Swift,Permissions,Location,Mapkit,获取有关请求位置权限的错误:“尝试在不提示位置授权的情况下启动MapKit位置更新。必须先调用-[CLLocationManager RequestWhenUseAuthorization]或-[CLLocationManager requestAlwaysAuthorization]”问题是我已经这么做了,在我为完全不同的东西添加url方案之前,它工作得很好 这在我的ViewController中: override func viewDidLoad() { super

获取有关请求位置权限的错误:“尝试在不提示位置授权的情况下启动MapKit位置更新。必须先调用-[CLLocationManager RequestWhenUseAuthorization]或-[CLLocationManager requestAlwaysAuthorization]”问题是我已经这么做了,在我为完全不同的东西添加url方案之前,它工作得很好

这在我的ViewController中:

override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.startUpdatingLocation()
        self.mapView.showsUserLocation = true
        //your loc
        mapView.delegate = self
        let initialLocation = CLLocation(latitude: 39, longitude: 77)
        centerMapOnLocation(initialLocation)
        }

您需要询问用户是否已授予跟踪其位置的权限:

if CLLocationManager.authorizationStatus() == .NotDetermined {
    manager.requestAlwaysAuthorization()
}
然后,您需要一个回调函数,该函数将在授予权限时调用:

func locationManager(manager: CLLocationManager,
                     didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
    if status == .AuthorizedAlways || status == .AuthorizedWhenInUse {
        manager.startUpdatingLocation()
        // ...
    }
}
请记住,您必须异步执行此操作,因为您知道何时请求权限(例如,当视图控制器加载时),但不知道用户何时单击“允许”按钮


当不使用UsageDescription时,不要忘记设置
nsLocation或
NSLocationAlwaysUsageDescription
,否则它将不会提示。

您需要询问用户是否已授予跟踪其位置的权限:

if CLLocationManager.authorizationStatus() == .NotDetermined {
    manager.requestAlwaysAuthorization()
}
然后,您需要一个回调函数,该函数将在授予权限时调用:

func locationManager(manager: CLLocationManager,
                     didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
    if status == .AuthorizedAlways || status == .AuthorizedWhenInUse {
        manager.startUpdatingLocation()
        // ...
    }
}
请记住,您必须异步执行此操作,因为您知道何时请求权限(例如,当视图控制器加载时),但不知道用户何时单击“允许”按钮


当不使用UsageDescription时,不要忘记设置
nsLocation,或者
NSLocationAlwaysUsageDescription
,否则它将不会提示。

错误是什么?为了帮助您,我们需要一些详细信息RequestalwaysAuthorization()向用户显示一个对话框,即使他们是世界上速度最快的用户,我认为他们也无法在下一行代码中执行startUpdatingLocation()之前的一小部分时间内接受该对话框。如果用户不授予权限怎么办?。关于如何实现请求授权,有数以百万计的教程和问题,请阅读这些教程和问题,它并不像您仅有的几行代码那么简单。错误是什么?为了帮助您,我们需要一些详细信息RequestalwaysAuthorization()向用户显示一个对话框,即使他们是世界上速度最快的用户,我认为他们也无法在下一行代码中执行startUpdatingLocation()之前的一小部分时间内接受该对话框。如果用户不授予权限怎么办?。关于如何实现请求授权,有数以百万计的教程和问题,请阅读它们,它并不像您拥有的几行代码那么简单。