Swift - CoreLocation: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „== Links == == Quickstart == Edit Info.plist um vom User nach zusätzlicher Erlaubnis für Location zu Fragen: Key: Privacy - Location When In Use Usage Desc…“)
 
Zeile 5: Zeile 5:
 
  Key: Privacy - Location When In Use Usage Description - Value z.B. We need your location to get the current weather for where you are.
 
  Key: Privacy - Location When In Use Usage Description - Value z.B. We need your location to get the current weather for where you are.
  
 +
WeatherViewController
 
<syntaxhighlight lang="swift">
 
<syntaxhighlight lang="swift">
 +
 +
import UIKit
 
import CoreLocation
 
import CoreLocation
// ..
 
class WeatherViewController: UIViewController {
 
  // ..
 
  var locationManager = CLLocationManager()
 
  // ..
 
  override func viewDidLoad() {
 
    // Ask user to access position (see also Info.plist)
 
    locationManager.requestWhenInUseAuthorization()
 
  
 +
class WeatherViewController: UIViewController{
 +
    // ..
 +
    var locationManager = CLLocationManager()
 +
   
 +
    override func viewDidLoad() {
 +
        // ..
 +
        // Delegate the locationManager to the view
 +
        locationManager.delegate = self
 +
        // Ask user to access position (see also Info.plist)
 +
        locationManager.requestWhenInUseAuthorization()
 +
        // get onetime location
 +
        locationManager.requestLocation()
 +
        // ..
 +
    }
 +
}
 +
 +
//MARK: - CLLocationManagerDelegate
 +
extension WeatherViewController: CLLocationManagerDelegate {
 +
   
 +
    // Button to get device location
 +
    @IBAction func getLocationTapped(_ sender: UIButton) {
 +
        print("get location tapped")
 +
        // get onetime location
 +
        locationManager.requestLocation() 
 +
    }
 +
    // didUpdateLocation delegate receiver
 +
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
 +
        print("updated location")
 +
        if let location = locations.last{
 +
            /**
 +
            we wanna stop the locationManager so next time we trigger it we will surely get a location
 +
            even location has not changed.
 +
            */
 +
            locationManager.stopUpdatingLocation()
 +
            let lat = location.coordinate.latitude
 +
            let lon = location.coordinate.longitude
 +
            weatherManager.fetchWeather(latitude: lat, longitude: lon)
 +
        }
 +
    }
 +
    // didFailWithError delegate receiver
 +
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
 +
        print("Error receiving location: \(error)")
 +
    }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Version vom 20. Januar 2023, 21:06 Uhr

Links

Quickstart

Edit Info.plist um vom User nach zusätzlicher Erlaubnis für Location zu Fragen:

Key: Privacy - Location When In Use Usage Description - Value z.B. We need your location to get the current weather for where you are.

WeatherViewController

import UIKit
import CoreLocation

class WeatherViewController: UIViewController{
    // ..
    var locationManager = CLLocationManager()
    
    override func viewDidLoad() {
        // ..
        // Delegate the locationManager to the view
        locationManager.delegate = self
        // Ask user to access position (see also Info.plist)
        locationManager.requestWhenInUseAuthorization()
        // get onetime location
        locationManager.requestLocation()
        // ..
    }
}

//MARK: - CLLocationManagerDelegate
extension WeatherViewController: CLLocationManagerDelegate {
    
    // Button to get device location
    @IBAction func getLocationTapped(_ sender: UIButton) {
        print("get location tapped")
        // get onetime location
        locationManager.requestLocation()  
    }
    // didUpdateLocation delegate receiver
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("updated location")
        if let location = locations.last{
            /**
             we wanna stop the locationManager so next time we trigger it we will surely get a location
             even location has not changed.
             */
            locationManager.stopUpdatingLocation()
            let lat = location.coordinate.latitude
            let lon = location.coordinate.longitude
            weatherManager.fetchWeather(latitude: lat, longitude: lon)
        }
    }
    // didFailWithError delegate receiver
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Error receiving location: \(error)")
    }
}