I'm new to OS X development, but I have some experience on iOS.
I want to get current location on OS X. I created super simple project, basicaly it's a blank project with core location code added. I use Swift language for development here.
Here is AppDelegate file:
import Cocoa
import CoreLocation
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, CLLocationManagerDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
if CLLocationManager.authorizationStatus().rawValue == 1 || CLLocationManager.authorizationStatus().rawValue == 2 {
return
}
print(CLLocationManager.authorizationStatus().rawValue)// log: 0
if CLLocationManager.locationServicesEnabled() {
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.startUpdatingLocation()
print("ok") //log: ok
}
}
func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
print("\(newLocation) \(oldLocation)")
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
print(locations)
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
print(status)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error)
}
}As you can see, all is pretty obvious here.
CLLocationManager.authorizationStatus is always returns me 0, (e.g. NotDetermined).
I'm expecting to see some iOS like promt to a user, but I don't see it. And I don't recive any location update. No one of this delegate methods are called. I don't understand where I made a mistake, I read Location and Maps Programming Guide and I think I made all steps like in the docs.
Hope somebody could help me with this.
I fixed my problem, but the "fix" is very doubtful.
In my code I changed
//To:
locationManager = CLLocationManager()
locationManager.delegate = self
if CLLocationManager.locationServicesEnabled() {
//From here:
// locationManager = CLLocationManager()
// locationManager.delegate = self
locationManager.startUpdatingLocation()
print("ok")
}And autorisation prompt started to show!
I tried code on MBP and iMac, both didn't show the prompt untill this change. After the changes all fine.
The strange thing is that if I return code back, like in the original post, all works fine now, prompt will be shown (tried on both Macs too).
I'm not sure what it was. The only question I have left: I don't see my project at Security & Privacy in System Preferences. Is it ok for development?