2016-02-10 57 views
0

我想獲取我的位置,以便檢查我距離某個經度和緯度有多遠。Swift沒有達到LocationManager功能

出於某種原因,程序從未到達LocationManager函數,我想抓住我的座標。

我已經在info.plist中設置了正確的屬性,並且應用程序確實要求獲得許可。如果您使用的是iOS 8.0及更高版本,你需要開始更新位置之前,要求准予

didFailWithError Error Domain=kCLErrorDomain Code=0 "(null)"

import UIKit 
import CoreLocation 
import MapKit 

class ViewController: UIViewController, CLLocationManagerDelegate { 


    var locationManager:CLLocationManager = CLLocationManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

      locationManager.desiredAccuracy = kCLLocationAccuracyBest 
      locationManager.delegate = self 

      locationManager.requestWhenInUseAuthorization() 
      locationManager.startUpdatingLocation() 
      print("location on") 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     print("REACHED FUNCTION") 

    } 


} 
+0

如果執行'的LocationManager(_:didFailWithError :) '它去那裏,而不是,如果是的話,什麼是錯誤? –

+0

[CLLocationManager didUpdateLocations未被調用]可能的重複(http://stackoverflow.com/questions/26201471/cllocationmanager-didupdatelocations-not-being-called) – Vizllx

+0

所以我得到了這個錯誤:didFailWithError錯誤域= kCLErrorDomain代碼= 0「 (null)「 – js091514

回答

0

我添加了一個函數來檢查錯誤,並得到了。您可以使用下面的代碼爲

if #available(iOS 8.0, *) { 
    locationManager.requestWhenInUseAuthorization() 
} 

上面的代碼將在viewDidLoad去。並實現以下的委託方法

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
    switch status { 
    case .AuthorizedWhenInUse: 
     locationManager.startUpdatingLocation() 
     break 
    case .AuthorizedAlways: 
     locationManager.startUpdatingLocation() 
     break 
    default: 
     break 
    } 
} 
+0

這不起作用......從來沒有達到這個功能 – js091514