2017-11-05 5 views
-1

Ich habe die folgende Location Klasse, die ich den aktuellen Standort zurückgeben möchte. Ich kann den aktuellen Standort jedoch nicht abrufen. Ich glaube, ich habe alle korrekten PLIST-Elemente hinzugefügt und Core Location Framework hinzugefügt. Wenn ich auf getLocation tippe, verschwinden die Berechtigungen und ich kann die Berechtigung nicht bestätigen.Swift kann `didUpdateLocations` nicht auslösen

Debugger gibt die folgende:

CLLocationManager.locationServicesEnabled() true 
location Services Enabled 
locationManager.delegate <Shot_On_Goal.Location: 0x1c001f560> 


@IBAction func getLocation(_ sender: Any) { 

    let location = Location() 
    location.getCurrentLocation() 

} 



import Foundation 
import MapKit 
import CoreLocation 

class Location: NSObject { 

    var locationManager: CLLocationManager! 

    func getCurrentLocation() { 

     print("class->Location->getCurrentLocation") 

     locationManager = CLLocationManager() 

     if (CLLocationManager.locationServicesEnabled()) 
     { 
      print("location Services Enabled") 

      locationManager.delegate = self 
      locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
      locationManager.requestWhenInUseAuthorization() 
      locationManager.startUpdatingLocation() 

     } else { 

      locationManager.stopUpdatingLocation() 
     } 

    } 

} // class 

extension Location: CLLocationManagerDelegate { 

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 

     print("class->Location->didFailWithError") 

     print("Error to update location \(error)") 
    } 

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 

     print("class->Location->didChangeAuthorization") 

     switch status { 
     case .notDetermined: 
      print("notDetermined") 
     case .restricted: 
      print("restricted") 
     case .denied: 
      print("denied") 
     case .authorizedAlways: 
      print("authorizedAlways") 
     case .authorizedWhenInUse: 
      print("authorizedWhenInUse") 
     } 
    } 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     print("class->Location->didUpdateLocations") 

     let locValue:CLLocationCoordinate2D = manager.location!.coordinate 
     print("locations = \(locValue.latitude) \(locValue.longitude)") 
    } 

} //extension 

Antwort

2

Das Problem mit diesen Code:

let location = Location() // <-- oops 
location.getCurrentLocation() 

Diese Location Instanz muss eine persistente beibehalten Objekt (zB eine globale oder eine Instanz Eigenschaft einiger persistent Controller oder Anwendungsdelegate anzeigen). Es kann keine lokale Variable sein, wie Sie sie haben; es verschwindet einfach, bevor es funktionieren kann.

+0

Siehe mein Beispiel hier: https://github.com/mattnub/Programming-iOS-Book-Examples/blob/master/bk2ch22p773location/ch35p1032location/ViewController.swift Ich mache etwas ganz ähnliches wie Sie, setzen Sie die ganze Ort Zeug in eine ManagerHolder-Klasse - aber sieh dir an, wie ich diese Klasse als Instanzeigenschaft meines Root-View-Controllers instanziiere. Deshalb funktioniert mein Code und deiner nicht. – matt

+0

Danke für den schnellen Fund, oops ist richtig ... –

Verwandte Themen