2016-12-14 29 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