2

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()
}
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Debasish Mitra
  • 1,394
  • 1
  • 14
  • 17
  • possible duplicate of [How can I get current location from user in iOS](http://stackoverflow.com/questions/4152003/how-can-i-get-current-location-from-user-in-ios) – Vivek Molkar May 06 '15 at 05:44
  • Even if you are building a framework, you still need to get authorised by the user in order to get the locations. Does your framework publish some method in order to manage the needed `CLLocationManager` authorisations (and availability)? – Matteo Piombo May 06 '15 at 05:46
  • Although I am new to iOS development but this code works fine if added inside the main application's ViewController. locationManager.requestWhenInUseAuthorization() will automatically ask for user's authorization using value for key NSLocationWhenInUseUsageDescription in info.plist file. But after adding the code to Framework user is not getting authorization message. – Debasish Mitra May 06 '15 at 05:54

1 Answers1

0

Finally found a solution that works.

Although the implementation might be a bit different for your scenario but take a look at this example. Thanks to the owner of the project.

https://github.com/icanzilb/OneShotLocationManager

Debasish Mitra
  • 1,394
  • 1
  • 14
  • 17