hi, so the user must click the POWER ON button in order to share the location every 5s
Longitude and Latitude
@IBAction func power(_ sender: UIButton) {
var helloWorldTimer = Timer.scheduledTimer(timeInterval: tiempou, target: self, selector: #selector(ConductorPrincipalViewController.sayHello), userInfo: nil, repeats: true)
}this is my #Selector
@objc func sayHello()
{
UserDefaults.standard.set(latitud.text, forKey: "latitudeConductor")
UserDefaults.standard.set(longitud.text, forKey: "longitudConductor")
let id = UserDefaults.standard.string(forKey: "idChofer")
let request = NSMutableURLRequest(url: NSURL(string: "hehe")! as URL)
request.httpMethod = "POST"
let postString = "Id=\(id!)&Latitud=\(latitud.text!)&Longitud=\(longitud.text!)" //location send to DB
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(String(describing: error))")
return
}
print("response = \(String(describing: response))")
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(String(describing: responseString))")
}
task.resume()
}everything is working fine.
But if the user decides not keep sharing the location
standBY buton
@IBAction func reposo(_ sender: UIButton) {
tiempou = 0.0
UserDefaults.standard.set(latitud.text, forKey: "latitudeConductor")
UserDefaults.standard.set(longitud.text, forKey: "longitudConductor")
let id = UserDefaults.standard.string(forKey: "idChofer")
let request = NSMutableURLRequest(url: NSURL(string: "hehe")! as URL)
request.httpMethod = "POST"
let postString = "Id=\(id!)&Latitud=\("")&Longitud=\("")" // the location is updated to ""(nothing)
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(String(describing: error))")
return
}
print("response = \(String(describing: response))")
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(String(describing: responseString))")
}
task.resume()
}i had tried multiple things
i set a variable to overwrite the time as 0 instead of 5
var tiempou = 5.0and when the user tap the standBY button
@IBAction func reposo(_ sender: UIButton) {
tiempou = 0.0time is changed to 0.0 but it doesn't work the location keeps sharing every 5s
Any ideas? 😕
my DidViewLoad in case need
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
map.delegate = self
map.mapType = .standard
map.isZoomEnabled = true
map.isScrollEnabled = true
map.showsUserLocation = true
if let coor = map.userLocation.location?.coordinate{
map.setCenter(coor, animated: true)
}
}