Ios CLLocation提示符立即显示并消失
在我的应用程序中,我尝试从GPS获取经度和纬度。要做到这一点,我必须询问用户访问其位置的权限。在此之前,我将这两条规则添加到Ios CLLocation提示符立即显示并消失,ios,swift,core-location,uialertcontroller,ios10,Ios,Swift,Core Location,Uialertcontroller,Ios10,在我的应用程序中,我尝试从GPS获取经度和纬度。要做到这一点,我必须询问用户访问其位置的权限。在此之前,我将这两条规则添加到Info.plist中:隐私-使用时的位置使用说明和隐私-位置始终使用说明,然后在AppDelegate中,我询问执行此操作的权限(SWIFT 3.0): 在运行应用程序时,我可以看到UIAlertController,但几乎在同一时间它消失了,我没有时间点击Allow,我也不能使用GPS。如何修复它 我的问题的工作解决方案: 我在类locationManager中创建了单
Info.plist
中:隐私-使用时的位置使用说明和隐私-位置始终使用说明,然后在AppDelegate
中,我询问执行此操作的权限(SWIFT 3.0):
在运行应用程序时,我可以看到UIAlertController
,但几乎在同一时间它消失了,我没有时间点击Allow
,我也不能使用GPS。如何修复它
我的问题的工作解决方案:
我在类locationManager中创建了单独的变量var locationManager=CLLocationManager()
,然后在函数中使用了它。
问题是locationManager对象在出现授权提示之前被解除分配RequestWhenUseAuthorization
以延迟方式运行,因此CLLocationManager
的此实例将从您的下方被拉出
因此,将LocalizationManager的作用域更改为视图控制器类,而不是局部变量
class ViewController: UIViewController {
let localisationManager = CLLocationManager() // <-- scope to class
//...
function requestAuthorization() {
localisationManager.requestWhenInUseAuthorization()
}
}
类ViewController:UIViewController{
让LocalizationManager=CLLocationManager()//只是一个额外的,下面是视频:
class ViewController: UIViewController {
let localisationManager = CLLocationManager() // <-- scope to class
//...
function requestAuthorization() {
localisationManager.requestWhenInUseAuthorization()
}
}