2016-02-13 76 views
3

我想在我的应用上使用后台获取位置更新服务。但是不要在这里显示我的代码,我想要你的帮助。使用核心位置进行Swift后台获取位置更新

import CoreLocation 

class ViewController: UIViewController, UITextFieldDelegate, CLLocationManagerDelegate { 

    var locationManager:CLLocationManager = CLLocationManager() 



    override func viewDidLoad() { 
     super.viewDidLoad() 


     self.locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.requestAlwaysAuthorization() 
     locationManager.startUpdatingLocation() 


     } 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
     print("didChangeAuthorizationStatus") 

     switch status { 
     case .NotDetermined: 
      print(".NotDetermined") 
      break 

     case .Authorized: 
      print(".Authorized") 
      self.locationManager.startUpdatingLocation() 
      break 

     case .Denied: 
      print(".Denied") 
      break 

     default: 
      print("Unhandled authorization status") 
      break 

     } 
    } 

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

     let location = locations.last as CLLocation! 

     print("didUpdateLocations: \(location.coordinate.latitude), \(location.coordinate.longitude)") 


    } 

给予只输出

didChangeAuthorizationStatus 
.NotDetermined 

如果可能的话我想多久纬度值时,新的位置变化。 谢谢!

+0

你必须添加到您的plist条目。 Google CoreLocation + plist –

回答

1

我已将此添加到Info.plist文件:

<key>NSLocationAlwaysUsageDescription</key> 
<string>Your message goes here</string> 
<key>NSLocationWhenInUseUsageDescription</key> 
<string>Your message goes here</string>