0

I want to track use location when user killed our app and store this (laongite and latitude) value into database using Alamofire.

class ViewController: UIViewController, CLLocationManagerDelegate {
    var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskIdentifier.invalid
    // Used to start getting the users location
    let locationManager = CLLocationManager()
    var longitude  = Double()
    var latitude = Double()
    var count = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        registerBackgroundTask()
        doSomeDownload()
        locationManager.requestAlwaysAuthorization()


        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.allowsBackgroundLocationUpdates = true
            locationManager.pausesLocationUpdatesAutomatically = false
            locationManager.startUpdatingLocation()
            locationManager.startMonitoringSignificantLocationChanges()
        }
    }

    // Print out the location to the console
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.first {
            print(location.coordinate)
           longitude = location.coordinate.longitude
           latitude = location.coordinate.latitude
            updatelocation()



        }
    }

    // If we have been deined access give the user the option to change it
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if(status == CLAuthorizationStatus.denied) {
            showLocationDisabledPopUp()
        }
    }

    // Show the popup to the user if we have been deined access
    func showLocationDisabledPopUp() {
        let alertController = UIAlertController(title: "Background Location Access Disabled",
                                                message: "In order to deliver pizza we need your location",
                                                preferredStyle: .alert)

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        alertController.addAction(cancelAction)

        let openAction = UIAlertAction(title: "Open Settings", style: .default) { (action) in
            if let url = URL(string: UIApplication.openSettingsURLString) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        }
        alertController.addAction(openAction)

        self.present(alertController, animated: true, completion: nil)
    }
    func doSomeDownload() {
        count = count + 1
        print("count")
        //call endBackgroundTask() on completion..
        switch UIApplication.shared.applicationState {
        case .active:
            print("App is active.")
        case .background:
            print("App is in background.")
            print("Background time remaining = \(UIApplication.shared.backgroundTimeRemaining) seconds")
        case .inactive:
            break


        @unknown default:
            fatalError()
            break
        }
    }

    func registerBackgroundTask() {

        backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
            self?.endBackgroundTask()

        }
        assert(backgroundTask != UIBackgroundTaskIdentifier.invalid)
    }

    func endBackgroundTask() {
        print("Background task ended.")
        UIApplication.shared.endBackgroundTask(backgroundTask)
        backgroundTask = UIBackgroundTaskIdentifier.invalid
    }




    func updatelocation()
    {



        print (longitude)
        print (latitude)

        let URL_USER_LOGIN = "http://etaksi.biz/TouchOne/ios/location_update.php"
        //getting the username and password
        let parameters: Parameters=[
            "id":"15350",
            "longitude":self.longitude,
            "latitude" : self.latitude

        ]
        Alamofire.request(URL_USER_LOGIN, method: .post, parameters: parameters).responseJSON
            {
                data in

        }




    }
}
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
Ali
  • 1
  • 1
  • Does this answer your question? [iOS GPS tracking app that runs all the time](https://stackoverflow.com/questions/35515528/ios-gps-tracking-app-that-runs-all-the-time) – Hitesh Surani Feb 06 '20 at 13:28
  • If you want to send user location continuously, Then it is not possible. If you want to access user location periodically then you can go with region monitoring and significant location update. – Hitesh Surani Feb 06 '20 at 13:30
  • can region monitoring and significant location update nwork when user killed our app? – Ali Feb 07 '20 at 20:21
  • Yes, Region monitoring and significant location update worked in kill state too. But frequent or continues location update is not possible with it – Hitesh Surani Feb 08 '20 at 07:07

0 Answers0