CocoaheadsSKG
Dimitri James Tsiflitzis
if CLLocationManager.authorizationStatus() == .NotDetermined {
manager.requestAlwaysAuthorization()
}
// OR
if CLLocationManager.authorizationStatus() == .NotDetermined {
manager.requestWhenInUseAuthorization()
}func locationManager(manager: CLLocationManager,
didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
if status == .AuthorizedAlways || status == .AuthorizedWhenInUse {
manager.startUpdatingLocation()
// ...
}
}let manager = CLLocationManager()
if CLLocationManager.locationServicesEnabled() {
manager.startUpdatingLocation()
}In your app .plist the values assigned to the
These keys are now mandatory.
switch CLLocationManager.authorizationStatus() {
case .AuthorizedAlways:
// ...
case .NotDetermined:
manager.requestAlwaysAuthorization()
case .AuthorizedWhenInUse, .Restricted, .Denied:
let alertController = UIAlertController(
title: "Background Location Access Disabled",
message: "Please open this app's settings and set location access to 'Always'.",
preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alertController.addAction(cancelAction)
let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(url)
}
}
alertController.addAction(openAction)
self.presentViewController(alertController, animated: true, completion: nil)
}This has been the location framework since iOS 8.0
tl;dr don't worry about it
class CLFloor : NSObject {
var level: Int { get }
}
Handling visits
Handling visits
manager.startMonitoringVisits()
func locationManager(manager: CLLocationManager, didVisit visit: CLVisit!) {
if visit.departureDate.isEqualToDate(NSDate.distantFuture()) {
// User has arrived, but not left, the location
} else {
// The visit is complete
}
}func getQuickLocationUpdate() {
// Request location authorization
self.locationManager.requestWhenInUseAuthorization()
// Request a location update
self.locationManager.requestLocation()
// Note: requestLocation may timeout and produce an error if authorization has not yet been granted by the user
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Process the received location update
}func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Get a fix on the user's location
...
// Stop location updates
self.locationManager.stopUpdatingLocation()
}
func getLocationUpdate() {
// Create a location manager object
self.locationManager = CLLocationManager()
// Set the delegate
self.locationManager.delegate = self
// Request location authorization
self.locationManager.requestWhenInUseAuthorization()
// Set an accuracy level. The higher, the better for energy.
self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
// Start location updates
self.locationManager.startUpdatingLocation()
}
extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);
extern const CLLocationAccuracy kCLLocationAccuracyBest;
extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;On devices with GPS hardware, you can let the location manager defer the delivery of location updates when your app is in the background.
func startHikeLocationUpdates() {
// Create a location manager object
self.locationManager = CLLocationManager()
// Set the delegate
self.locationManager.delegate = self
// Request location authorization
self.locationManager.requestWhenInUseAuthorization()
// Specify the type of activity your app is currently performing
self.locationManager.activityType = CLActivityTypeFitness
// Start location updates
self.locationManager.startUpdatingLocation()
}func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// Add the new locations to the hike
self.hike.addLocations(locations)
// Defer updates until the user hikes a certain distance or a period of time has passed
if (!deferringUpdates) {
distance: CLLocationDistance = hike.goal - hike.distance
time: NSTimeInterval = nextUpdate.timeIntervalSinceNow()
locationManager.allowDeferredLocationUpdatesUntilTraveled(distance, timeout:time)
deferringUpdates = true;
} }
func locationManager(manager: CLLocationManager, didFinishDeferredUpdatesWithError error: NSError!) {
// Stop deferring updates
self.deferringUpdates = false
// Adjust for the next goal
}
NOTE
The monitoring techniques described below provide entry and exit notifications only. To determine the user’s actual location when a notification is received, you must call the requestLocation: or startUpdatingLocation: method of the location manager object. These monitoring techniques also require an authorization status of kCLAuthorizationStatusAuthorizedAlways.
let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude,
longitude: coordinate.longitude), radius: regionRadius, identifier: title)
locationManager.startMonitoringForRegion(region)
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
showAlert("enter \(region.identifier)")
}
// 2. user exit region
func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
showAlert("exit \(region.identifier)")
}NOTE
These monitoring techniques also require an authorization status of kCLAuthorizationStatusAuthorizedAlways.
// Somewhere
[locationManager startMonitoringSignificantLocationChanges];
// didFinishLaunching
if([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.activityType = CLActivityTypeAutomotiveNavigation;
locationManager.allowsBackgroundLocationUpdates = YES;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.pausesLocationUpdatesAutomatically = NO;
}
// location manager delegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
}If GPS-level accuracy isn’t critical for your app, you don’t need continuous tracking, and region or visit monitoring isn’t more appropriate for your app, you can use the significant-change location service instead of the standard one.