Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

361
Views
¿Cómo guardo la ubicación actual del usuario en los valores predeterminados?

Estoy tratando de dar a los usuarios la opción de guardar su ubicación actual. Usé los valores predeterminados para guardarlo y descubrí cómo obtener la ubicación de los usuarios. Pero no estoy seguro de cómo combinar esas dos tareas. Ésta es mi pregunta:

¿Cómo puedo hacer que mi función llame a la ubicación de los usuarios para que pueda guardarse?

 @IBAction func addLocation(_ sender: Any) { // CALL LOCATION MANAGER TO GET LOCATION IN LAT AND LONG self.saveDefaults() } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return } print("locations = \(locValue.latitude) \(locValue.longitude)") } func saveDefaults() { UserDefaults.standard.set(self.pickers, forKey: "pickers") print("SAVED PICKERS: \(pickers)") }

Este es mi intento más reciente, pero no sé por qué mi estructura (lat:, long:) no está tomando la entrada

 @IBAction func addLocation(_ sender: Any) { var locations: [CLLocation] let manager: CLLocationManager let userLocation:CLLocation = locations[0] as CLLocation // Call stopUpdatingLocation() to stop listening for location updates, // other wise this function will be called every time when user location changes. manager.stopUpdatingLocation() print("user latitude = \(userLocation.coordinate.latitude)") print("user longitude = \(userLocation.coordinate.longitude)") self.pickers.append(pickerStruct(lat: userLocation.coordinate.latitude, long: userLocation.coordinate.longitude)) self.saveDefaults() }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

@IBAction func addLocation(_ sender: Any) { locationManager.startUpdatingLocation() //This will call the delegate method below where you can save the location } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard locations.count > 0 else { return } guard let location = locations.last else { return } locationManager.stopUpdatingLocation() //Stop location updates after getting the location and save that location as below let encodedLocation = NSKeyedArchiver.archivedData(withRootObject: location) UserDefaults.standard.set(encodedLocation, forKey: "savedLocation") }

Para recuperar la ubicación de UserDefaults:

 let previousLocationEncoded = UserDefaults.standard.object(forKey: "savedLocation") as? Data let previousLocationDecoded = NSKeyedUnarchiver.unarchiveObject(with: previousLocationEncoded!) as! CLLocation
over 4 years ago · Santiago Trujillo Report

0

Las funciones utilizadas en la respuesta de Sujal están en desuso. Aquí está la versión de trabajo actual (2021) de las funciones:

 // Save location to UserDefaults if let encodedLocation = try? NSKeyedArchiver.archivedData(withRootObject: location, requiringSecureCoding: false) { UserDefaults.standard.set(encodedLocation, forKey: "savedLocation") } // Load the last saved location if let loadedLocation = UserDefaults.standard.data(forKey: "savedLocation"), let decodedLocation = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(loadedLocation) as? CLLocation { // Use the decodedLocation as you want }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!