I have created a framework in swift. But I am trying to get location values - lat and lon within the framework. The below code compiles fine but the location values are not getting populated. I have already added NSlocationWheninUseUSageDescription key to the info.plist.
Can some tell me is it even possible to get location values within the framework? If yes, then what should be done here?
Note: This code works fine if added to main application's ViewController class. But the user is not getting the authorization message if this code is added to framework and called from there.
import Foundation
import CoreLocation
public class FrameworkInit : NSObject, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var lat = ""
var long = ""
var locflag = false
var _appKey = ""
func fetchLocation()
{
println("fetch loc called....")
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
public func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
var locValue:CLLocationCoordinate2D = manager.location.coordinate
lat = locValue.latitude.description
long = locValue.longitude.description
println("lat.....\(lat) \nlong...... \(long)")
self.locationManager.stopUpdatingLocation()
if !locflag {
SPDeviceRegister().saveDataOnDevice(_appKey, lat: lat, long: long)
SPReceiver().thresholdValueSetter()
}
locflag = true
}
public func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
println("Error:" + error.localizedDescription)
if !locflag {
SPDeviceRegister().saveDataOnDevice(_appKey, lat: "0.0", long: "0.0")
SPReceiver().thresholdValueSetter()
}
locflag = true
}
public init(appKey: String)
{
super.init()
_appKey = appKey
self.fetchLocation()
}
}