Do I need to use a single-point CLLocationManager in order not to wait for the device location update?

I have heard over and over again that there is always a better model than singleton, but I can’t figure out how else my application can access the device’s location without waiting for the GPS data to return (I assume that the location system only works when explicitly calling correct me if this is not so).

So, is there a better template for accessing CLLocation data from multiple (unrelated) controllers? Or can I expect the device’s location to be updated in the background, even if I can’t access it through the CLLocationManager?

+4
source share
1 answer

Declare one class. Like the following.

MyLocation.h

@protocol MyCLControllerDelegate <NSObject> - (void)locationUpdate:(CLLocation *)location; - (void)locationError:(NSError *)error; @end @interface MyLocation : NSObject <CLLocationManagerDelegate> { CLLocationManager *locationManager; id delegate; } @property (nonatomic, strong) CLLocationManager *locationManager; @property (nonatomic, strong) id <MyCLControllerDelegate> delegate; 

MyLocation.m

 #import "MyLocation.h" @implementation MyLocation @synthesize locationManager; @synthesize delegate; - (id) init { self = [super init]; if (self != nil) { if([CLLocationManager locationServicesEnabled]) { if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted ) { [self showAlertWithTitle:@"Warning" andWithMessage:@"Determining your current location cannot be performed at this time because location services are enabled but restricted" forTargetView:self]; NSlog(@"Determining your current location cannot be performed at this time because location services are enabled but restricted"); } else { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; // send loc updates to myself [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; [self.locationManager setDistanceFilter:kThresholdDistance]; [self.locationManager startUpdatingLocation]; NSLog(@"Location sharing set ON!"); } } else { [MobileYakHelper showAlertWithTitle:@"Error" andWithMessage:@"Determining your current location cannot be performed at this time because location services are not enabled." forTargetView:self]; NSLog(@"Location sharing set OFF!"); } } return self; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSDictionary *dictValue = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:newLocation.coordinate.latitude], @"latitude", [NSNumber numberWithDouble:newLocation.coordinate.longitude], @"longitude", nil]; [[NSUserDefaults standardUserDefaults] setValue:dictValue forKey:@"MY_LOCATION"]; CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation]; if (meters >= kThresholdDistance ) { [self.delegate locationUpdate:newLocation]; } } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { [self.delegate locationError:error]; } @end 

To use it in multiple controllers. Add the .h file to it and use it as follows:

 - (void) initializeLocations { MyLocation _myLocation = [[MyLocation alloc] init]; _myLocation.delegate = self; _myLocation.locationManager.desiredAccuracy = kCLLocationAccuracyBest; CLLocation * currentLocation = _myLocation.locationManager.location; // Updating user current location to server [self sendUserCurrentCoordinate:currentLocation.coordinate]; // start updating current location _myLocation.locationManager.desiredAccuracy = kCLLocationAccuracyBest; [_myLocation.locationManager startUpdatingLocation]; [_myLocation.locationManager startUpdatingLocation]; } - (void)locationUpdate:(CLLocation *)location { NSLog(@"location %@", location); } - (void)locationError:(NSError *)error { NSLog(@"locationdescription %@", [error description]); } 
+1
source

All Articles