1

I'm trying to get geolocation data in an arbitrary class. I'm very new to Swift, so I have no idea why this isn't working?

Any pointers?

import Foundation
import UIKit
import CoreLocation

class GeolocationPlugin:NSObject, CLLocationManagerDelegate {
  var locationManager: CLLocationManager!
  var lat: Double = 0
  var long: Double = 0

  func getLocation() {
    print("Getting location")

    // For use in foreground
    self.locationManager = CLLocationManager()
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
//    locationManager.startMonitoringSignificantLocationChanges()


    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError) {
      print("Error while updating location " + error.localizedDescription)
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]) {
      let locValue:CLLocationCoordinate2D = manager.location!.coordinate
      print("locations = \(locValue.latitude) \(locValue.longitude)")
    }


    self.locationManager.requestLocation()

    print("gets here")
  }
}

I currently see Getting location and then an error:

2017-03-26 15:42:32.634 IonicRunner[42304:5668243] *** Assertion failure in -[CLLocationManager requestLocation], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework_Sim/CoreLocation-2100.0.34/Framework/CoreLocation/CLLocationManager.m:865
2017-03-26 15:42:32.638 IonicRunner[42304:5668243] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didUpdateLocations:'
Perry
  • 869
  • 9
  • 14

1 Answers1

0

The solution wound up being to move the methods out of getLocation(), to properly activate a location in the simulator, and to move where this class was initiated from, so it wasn't immediately released as soon as getLocation() completes.

import Foundation
import UIKit
import CoreLocation

class GeolocationPlugin:NSObject, CLLocationManagerDelegate {
  var locationManager = CLLocationManager()
  var lat: Double = 0
  var long: Double = 0
  var cb: ((Double, Double) -> Void)? = nil

  func getLocation(callback: @escaping (Double, Double) -> Void) {
    print("Getting location")

    // For use in foreground
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

    self.locationManager.requestLocation()
    self.cb = callback
  }

  func locationManager(_ manager: CLLocationManager, didFailWithError error: NSError) {
    print("Error while updating location " + error.localizedDescription)
  }

  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate
    //print("locations = \(locValue.latitude) \(locValue.longitude)")

    if( self.cb != nil) {
      self.cb!(locValue.latitude, locValue.longitude)
    }
  }
}
Perry
  • 869
  • 9
  • 14