Search code examples
iosswiftxcode7cllocationmanagerbackground-fetch

Swift background fetch location updates with Core Location


I want to use Background Fetch Location Updates service on my app. But don't show any output my codes here i want your help.

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)")


    }

Giving output only

didChangeAuthorizationStatus
.NotDetermined

If it possible i want to take long lat value when new location changes. Thank you !


Solution

  • I added this to the info.plist file:

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