Reputation: 3749
I am creating an iOS Framework and i want to use Core Location to interact with Beacons. For testing reasons i am trying to get user location.
This is the class i created in the framework.
import Foundation
import CoreLocation
public class BeaconManager:NSObject,CLLocationManagerDelegate{
var locationManager:CLLocationManager = CLLocationManager()
public override init() {
super.init()
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.startUpdatingLocation()
}
public func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
println(location)
}
}
}
And i am calling it from a test app that has the framework like this
import UIKit
import OtravitaSDK
import CoreLocation
class ViewController: UIViewController {
var bm = BeaconManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
But is not working , is not printing the location. I have set the NSLocationAlwaysUsageDescription both in framework's info.plist and the app's info.plist
Upvotes: 1
Views: 1374
Reputation: 186
You can create at first the reporter class (with shared instance) which will implement the CLLocationManagerDelegate, so you could implement your logic inside delegate methods
import Foundation
import CoreLocation
class LocationReporter: NSObject, CLLocationManagerDelegate {
static let sharedInstance = LocationReporter()
func startUpdating(locationManager: CLLocationManager) {
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
func stopUpdating(locationManager: CLLocationManager) {
locationManager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
print("latitude: ", location.coordinate.latitude)
print("longitude: ", location.coordinate.longitude)
}
}
//implement other locationManger delegate methods
}
Next you can create a Client class
import Foundation
import CoreLocation
class LocationDetectionClient {
private let locationManager = CLLocationManager()
func start() {
LocationReporter.sharedInstance.startUpdating(locationManager: locationManager)
}
func stop() {
LocationReporter.sharedInstance.stopUpdating(locationManager: locationManager)
}
}
And finally call the Client methods where you need
let locationDetectionClient = LocationDetectionClient()
public func startLocationDetection() {
locationDetectionClient.start()
}
public func stopLocationDetection() {
locationDetectionClient.stop()
}
Hope this would help
Upvotes: 0
Reputation: 641
you can add your decription in NSLocationAlwaysUsageDescription
& NSLocationWhenInUseUsageDescription
in plist
This code put into AppDelegate
file
var locationManager:CLLocationManager?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//You can give this permission for fetch current location
var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound;
var setting = UIUserNotificationSettings(forTypes: type, categories: nil);
UIApplication.sharedApplication().registerUserNotificationSettings(setting);
UIApplication.sharedApplication().registerForRemoteNotifications();
locationManager = CLLocationManager()
locationManager?.requestAlwaysAuthorization()
locationManager?.delegate = self
locationManager?.startUpdatingLocation()
// Override point for customization after application launch.
return true
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
println(location)
}
}
Upvotes: 1
Reputation: 597
What you need to do as of iOS 8 is configure your Info.plist file
to cater for 2 kinds of location behaviour. You need to supply a default message that appears with a popup by default, asking the user for consent to use their location.
NSLocationWhenInUseUsageDescription
and NSLocationAlwaysUsageDescription
See this article for a full walkthrough and another SO post which discusses this topic. Hope this helps!
Upvotes: 0