1

Background location not working after kill/ Terminate application. I am using in my application NSLocationAlwaysUsageDescription and Background fetch also on from Capabilities, here my code is :

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate{

    var locationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 1000.0
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.startUpdatingLocation()
    }

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


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

        if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
            CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways){

           let currentLocation = manager.location
            let notification = UILocalNotification()
            notification.alertBody = " Lat \(currentLocation?.coordinate.latitude), Long \(currentLocation?.coordinate.longitude)"
            notification.soundName = UILocalNotificationDefaultSoundName
            UIApplication.sharedApplication().scheduleLocalNotification(notification)

            print(" Lat \((currentLocation?.coordinate.latitude))!, Long \((currentLocation?.coordinate.longitude))!")
        }

    }

}

this code is working when click home button but after terminate/kill app in this condition not working please give me the solution here what i missed inside my code.

Anand Nimje
  • 6,163
  • 4
  • 24
  • 43
  • Hello Rob now i got the solution and my code working fine when app in kill/ terminate right now and able to call web service and location also. – Anand Nimje Jul 13 '16 at 09:19
  • Updated code is not working in the terminated state. Used Xcode 8 and iOS 9.3.5 device. Have written all codes in the view controllers only. Anything i need to write on the app delegate. Please help me. – Prakash Nov 09 '16 at 12:47

1 Answers1

1

After some changes i got solution my self by doing this changes and code working fine in background mode and foreground mode now able to fetch location.

Swift 3

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = 2000
    if #available(iOS 9.0, *) {
        locationManager.allowsBackgroundLocationUpdates = true
    }
    locationManager.startMonitoringSignificantLocationChanges()
    locationManager.pausesLocationUpdatesAutomatically = true
    locationManager.requestAlwaysAuthorization()
Anand Nimje
  • 6,163
  • 4
  • 24
  • 43
  • ..Do you know the reason why updated code of yours is working?? If not then read the Apple doc. There is no need to use both startUpdatingLocation() and startMonitoringSignificantLocationChanges(). You just need to use startMonitoringSignificantLocationChanges() to get your task done.You can ask me in case of any doubt. – Kirti Nov 07 '16 at 11:39
  • Above code is not working in terminated state. Could you help me? Xcode 8 and 9.3.5 device – Prakash Nov 09 '16 at 12:41
  • Yes, i will update above code for Swift 3 after some time. – Anand Nimje Nov 09 '16 at 12:46
  • Have written all codes in the view controllers only. Anything i need to write on the appdelegate.swift for location tracking on the terminated state. Please help me. – Prakash Nov 09 '16 at 12:50
  • You can use this code both way with the use of Singleton class and the above code as it is also work because delegate method will always call when you distance cover. Every time delegate will give value in background and foreground but you need to manage it with all conditions. I have done this thing and working well in iOS 10 also. – Anand Nimje Nov 09 '16 at 16:51