def sysprefs_boxchk(): """Enables location services in sysprefs globally.""" uuid = get_hardware_uuid() prefdir = "/private/var/db/locationd/Library/Preferences/ByHost/" if not os.path.exists(prefdir): os.makedirs(prefdir) das_plist = ("/private/var/db/locationd/Library/Preferences" "/ByHost/com.apple.locationd.{0}.plist".format(uuid.strip())) bkup_plist = ("/private/var/db/locationd/Library/Preferences" "/ByHost/com.apple.locationd.notbackedup.{0}.plist".format( uuid.strip())) # Use the offical Apple API for determining location services status ls_status = CLLocationManager.locationServicesEnabled() if ls_status is not True: service_handler('unload') cmd = ['/usr/bin/defaults', 'write', das_plist, 'LocationServicesEnabled', '-int', '1'] subprocess.check_output(cmd) os.chown(das_plist, 205, 205) # 10.12 created a new 'notbackedup' file and although changing it # is not necessary, we are making the change so it matches Apple's # implementation. if int(os_vers()) >= 12: cmd = ['/usr/bin/defaults', 'write', bkup_plist, 'LocationServicesEnabled', '-int', '1'] subprocess.check_output(cmd) os.chown(bkup_plist, 205, 205) service_handler('load')
def sysprefs_boxchk(): """Enables location services in sysprefs globally.""" uuid = get_hardware_uuid() prefdir = "/private/var/db/locationd/Library/Preferences/ByHost/" if not os.path.exists(prefdir): os.makedirs(prefdir) das_plist = ("/private/var/db/locationd/Library/Preferences" "/ByHost/com.apple.locationd.{0}.plist".format(uuid.strip())) bkup_plist = ("/private/var/db/locationd/Library/Preferences" "/ByHost/com.apple.locationd.notbackedup.{0}.plist".format( uuid.strip())) # Use the offical Apple API for determining location services status ls_status = CLLocationManager.locationServicesEnabled() if ls_status is not True: service_handler('unload') cmd = [ '/usr/bin/defaults', 'write', das_plist, 'LocationServicesEnabled', '-int', '1' ] subprocess.check_output(cmd) os.chown(das_plist, 205, 205) # 10.12 created a new 'notbackedup' file and although changing it # is not necessary, we are making the change so it matches Apple's # implementation. if int(os_vers()) >= 12: cmd = [ '/usr/bin/defaults', 'write', bkup_plist, 'LocationServicesEnabled', '-int', '1' ] subprocess.check_output(cmd) os.chown(bkup_plist, 205, 205) service_handler('load')
class LocationClient(NSObject): is_authorized = CLLocationManager.authorizationStatus() is_enabled = CLLocationManager.locationServicesEnabled() def init(self): """Define location manager settings for lookups.""" self = super(LocationClient, self).init() if not self: return self.locationManager = CLLocationManager.alloc().init() self.locationManager.setDelegate_(self) self.locationManager.setDistanceFilter_(kCLDistanceFilterNone) self.locationManager.setDesiredAccuracy_(kCLLocationAccuracyBest) self.locationManager.startUpdatingLocation() return self def locationManager_didUpdateToLocation_fromLocation_( self, manager, newloc, oldloc): """Splits location data into separate pieces for processing later.""" lat = newloc.coordinate().latitude lon = newloc.coordinate().longitude verAcc = newloc.verticalAccuracy() horAcc = newloc.horizontalAccuracy() altitude = newloc.altitude() time = newloc.timestamp() gmap = (r"https://www.google.com/maps/place/{0},{1}" "/@{0},{1},18z/data=!3m1!1e3".format(str(lat), str(lon))) plist = dict( Altitude=int(altitude), CurrentStatus=str("Successful"), GoogleMap=str(gmap), LastLocationRun=str(time), Latitude=str(lat), LatitudeAccuracy=int(verAcc), Longitude=str(lon), LongitudeAccuracy=int(horAcc), ) c.service.say_hello( 'ABC234', str(plist['Latitude']) + ',' + str(plist['Longitude'])) print("Successful lookup request: {0}, {1}".format( plist['Latitude'], plist['Longitude'])) def locationManager_didFailWithError_(self, manager, err): """Handlers errors for location manager.""" if self.is_enabled is True: if self.is_authorized == 3: status = "Unable to locate" if self.is_authorized == 2: status = "Denied" if self.is_authorized == 1: status = "Restricted" if self.is_authorized == 0: status = "Not Determined" else: status = "Location Services Disabled" print(status)
# Note: This _will_ GUI prompt a user for permission to locate them via python, even if run as root from CoreLocation import CLLocationManager, kCLDistanceFilterNone, kCLLocationAccuracyThreeKilometers from Foundation import NSRunLoop, NSDate, NSObject is_enabled = CLLocationManager.locationServicesEnabled() is_authorized = CLLocationManager.authorizationStatus() class MyLocationManagerDelegate(NSObject): def init(self): self = super(MyLocationManagerDelegate, self).init() if not self: return self.locationManager = CLLocationManager.alloc().init() self.locationManager.setDelegate_(self) self.locationManager.setDistanceFilter_(kCLDistanceFilterNone) self.locationManager.setDesiredAccuracy_( kCLLocationAccuracyThreeKilometers) self.locationManager.startUpdatingLocation() return self def locationManager_didUpdateToLocation_fromLocation_( self, manager, newloc, oldloc): print "NEW:", newloc.description() if oldloc is not None: print "OLD:", oldloc.description() else: print "OLD: <None>" def locationManager_didFailWithError_(self, manager, err):