Я хочу отслеживать местоположение использования, когда пользователь убил наше приложение, и сохранить это значение (laongite и latitude) в базе данных, используя 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
}
}
}