2017-08-19 36 views
1

所以我幾個月前寫了這段代碼(當iOS 10.1還是最新版本時),並且它工作正常。但是,當我最近嘗試使用iOS 10.2時,CLLocationManager似乎沒有調用didUpdateLocation函數。當我將模擬器恢復到iOS 10.1時,它再次運行。CLLocationManager不調用iOS 10.2中的didUpdateLocation()[Swift]

當我試圖查找我的問題時,我所得到的只是iOS 8更新的解決方案,您還必須更新您的info.plist。因此,我想知道是否有額外的東西需要CoreLocation for iOS 10.2。我在info.plist中有Privacy - Location When In Use Usage DescriptionPrivacy - Location Always Usage Description信息屬性。

let locationManager = CLLocationManager() 
var userLocation:CLLocation = CLLocation() 

func handleSwitchChanged() { 
    if onlineSwitch.isOn { 
     if CLLocationManager.authorizationStatus() != .authorizedAlways { 
      locationManager.requestWhenInUseAuthorization() 
      onlineSwitch.isOn = false 
      return 
     } 
     (mainScreen as! MainScreenController).currUser.isOnline = 1 
     determineUserLocation() 
    } else { 
     (mainScreen as! MainScreenController).currUser.isOnline = 0 
     removeFromFirebase() 
    } 
} 

func determineUserLocation() { 
    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.requestAlwaysAuthorization() 

    if CLLocationManager.locationServicesEnabled() { 
     locationManager.startUpdatingLocation() 
    } 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    userLocation = locations[0] as CLLocation 
} 
+0

你在哪裏設置'委託'?我的意思是...調用'handleSwitchChanged()'調用'determineUserLocation()'?另外,不應該'!= .authorizedAlways'是'!= .authorizedWhenInUse'? –

+0

@AlejandroIván我在'determineUserLocation()'中設置委託,每當按下按鈕時都會調用委託。我認爲它是'.authorizedAlways'。我嘗試了'.authorizedWhenInUse',它不起作用。 – kbunarjo

+0

是的,但你正在請求使用授權。這意味着它永遠不會總是,因此,它會一直進入那個「如果」,然後返回。嘗試刪除該返回,以便您的函數實際調用determineUserLocation() –

回答

0

我認爲這可能只是iOS 10.2的錯誤。當我在10.1和10.3上嘗試相同的代碼時,它工作正常。

相關問題