ios – Swift后台使用Core Location获取位置更新

我想在我的应用上使用后台获取位置更新服务.但是不要在这里显示任何输出我的代码我想要你的帮助.

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

如果有可能我想在新位置改变时采用长拉特值.
谢谢 !

最佳答案 我把它添加到info.plist文件中:

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