2016-12-14 49 views
0

使用位置管理器有一些问题。 看起来像locationManager.requestWhenInUseAuth不会停止应用程序流,并且在用户可以解除身份验证警报之前调用startUpdatingLocation。 如何避免这种情况?CLLocationManager在用户授权之前开始更新Swift

我的应用程序加载了非可用GPS的默认值,所以我总是得到默认值(因为即使“想要认证此应用程序...?”警报仍然显示,此func被调用)。

我的代码:

if ask{ 
     locationManager.requestWhenInUseAuthorization() 
     self.manageLocation() 
    } 

func manageLocation(){ 
    if CLLocationManager.locationServicesEnabled() { 

     switch(CLLocationManager.authorizationStatus()) { 

     case .notDetermined, .restricted, .denied: 
      // load default deck 
      self.loadBlink(useDefault: true) 

     case .authorizedAlways, .authorizedWhenInUse: 
      locationManager.delegate = self 
      locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
      locationManager.distanceFilter = 10.0 
      locationManager.startUpdatingLocation() 

     } 
    } else { 
     // load default deck 
     self.loadBlink(useDefault: true) 
    } 
} 
+0

您是否在应用程序委托中放置了位置访问身份验证和startUpdatingLocation的代码? –

+0

如果您之前已经获得许可,位置管理器将开始更新位置。 –

+0

不,我应该吗? @HimanshuMoradiya –

回答

-1

添加到这一点您的info.plist

<key>NSLocationAlwaysUsageDescription</key> 
<string>$(PRODUCT_NAME) would like to use your location.</string> 
<key>NSLocationWhenInUseUsageDescription</key> 
<string>$(PRODUCT_NAME) would like to use your location.</string> 

设置var locationManager

var locationManager:CLLocationManager! 

viewDidLoad调用此:

locationManager = [[CLLocationManager alloc] init]; // objective-c 
locationManager = CLLocationManager() // swift (if i'm not wrong) 

调用这个requestWhenInUseAuth打电话之前startUpdatingLocation

+0

已经完成! @Hezron –

+0

然后尝试卸载并重新安装您的应用程序。 – Hezron

+0

我做了,仍然没有任何@Hezron –

0

在异步请求授权。它立即返回,你必须考虑到这一点。

+0

不好意思,这是什么意思? @Andreas –

+0

https://en.wikipedia.org/wiki/Asynchronous_method_invocation表示用户完成回答问题时得到回调。您不会阻止线程等待用户。 – Andreas