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

时间:2016-02-13 14:58:25

标签: ios swift xcode7 cllocationmanager background-fetch

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

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

如果有可能我想在新位置发生变化时采用长拉特值。 谢谢!

1 个答案:

答案 0 :(得分:1)

我将此添加到info.plist文件中:

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