2017-02-07 4 views
0

Ich entwickle eine App, in der ich den Standort des Benutzers regelmäßig aktualisieren muss, aber ich kann keine Standortaktualisierung erhalten, wenn die App gesperrt ist im Hintergrund laufen. Ich habe auch Hintergrund Standort Update in Fähigkeiten aktiviert.Wie bekomme ich eine Standortaktualisierung in ios mit swift, auch wenn die App im Hintergrund läuft?

viewDidLoad Code:

override func viewDidLoad() { 
     super.viewDidLoad() 
     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.allowsBackgroundLocationUpdates = true 
     locationManager.requestAlwaysAuthorization() 
     locationManager.requestWhenInUseAuthorization() 
     let user=FIRAuth.auth()?.currentUser?.displayName 
     if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse){ 
      locationManager.requestLocation() 
      location=locationManager.location 
      locationManager.startUpdatingLocation() 
      locationManager.startMonitoringSignificantLocationChanges() 
      //updating to database 
      ref.child("\(user!)/lat").setValue(location.coordinate.latitude) 
      ref.child("\(user!)/long").setValue(location.coordinate.longitude) 
      //location=locationManager.location 
     } 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     location=locations[locations.count-1] 
    } 
+0

Hoffnung diese Antwort würde Ihnen helfen, http://stackoverflow.com/questions/5323634/ios-application-executing-tasks-in-background – iMuzahid

+0

ich denke, diese Verbindung für Sie hilfreich http://stackoverflow.com/questions/28503990/ios-running-background-task-for-update-user-location-using-swift – Ram

+0

@Prakhar bro müssen Sie über diese https://github.com/ voyage11/Location –

Antwort

0

aktiviert Location Update -> Hintergrundmodus -> Capabilities

Ich wurde mit dieser Methode Lage im Hintergrund erhalten. Diese Methode schreiben in AppDelegate Klasse

func getLocation() { 
    self.locationManager = CLLocationManager() 
    self.locationManager.delegate = self 
    if self.locationManager.responds(to: #selector(self.requestAlwaysAuthorization)) { 
     self.locationManager.requestAlwaysAuthorization() 
    } 
    self.locationManager.distanceFilter = kCLDistanceFilterNone 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    if !CLLocationManager.locationServicesEnabled() { 
      // location services is disabled, alert user 
     var servicesDisabledAlert = UIAlertView(title: "Alert", message: "Location service is disable. Please enable to access your current location.", delegate: nil, cancelButtonTitle: "OK", otherButtonTitles: "") 
     servicesDisabledAlert.show() 
    } 
    else { 
     self.locationManager.startUpdatingLocation() 
    } 
} 
+1

@EricAya aktualisiert –

Verwandte Themen