Last active
December 23, 2015 02:59
-
-
Save ccabanero/6570684 to your computer and use it in GitHub Desktop.
iOS - Core Location - sample code for initializing Core Location and implementing CLLocationManagerDelegate delegate methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*---------------------------------------------------------------------------------------------------------------------------------- | |
Description: | |
iOS - Core Location - sample code for initializing Core Location and implementing CLLocationManagerDelegate delegate methods. | |
----------------------------------------------------------------------------------------------------------------------------------*/ | |
//interface file ... | |
#import <UIKit/UIKit.h> | |
#import <CoreLocation/CoreLocation.h> | |
@interface MyViewController : UIViewController<CLLocationManagerDelegate> | |
@end | |
//implementation file ... | |
@interface MyViewController () | |
@property (nonatomic, strong) CLLocationManager *locationManager; | |
- (void)initializeLocationManager; | |
@end | |
@implementation MyViewController | |
@synthesize locationManager = _locationManager; | |
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil | |
{ | |
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | |
if (self) | |
{ | |
// Custom initialization | |
} | |
return self; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[self initializeLocationManager]; | |
} | |
- (void)viewWillAppear:(BOOL)animated | |
{ | |
if(self.locationManager) | |
{ | |
[self.locationManager startUpdatingLocation]; | |
} | |
} | |
- (void)viewWillDisappear:(BOOL)animated | |
{ | |
if(self.locationManager) | |
{ | |
[self.locationManager stopUpdatingLocation]; | |
} | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
if(self.locationManager) | |
{ | |
[self.locationManager stopUpdatingLocation]; | |
self.locationManager = nil; | |
} | |
} | |
#pragma mark - Class Extension methods | |
- (void)initializeLocationManager | |
{ | |
self.locationManager = [[CLLocationManager alloc] init]; | |
self.locationManager.delegate = self; | |
[self.locationManager setActivityType:CLActivityTypeOtherNavigation]; //change as needed | |
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; //change as needed | |
[self.locationManager setDistanceFilter:50.0]; //change as needed | |
} | |
#pragma mark - CLLocationManagerDelegate methods | |
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { | |
CLLocation *newLocation = [locations lastObject]; | |
CLLocationCoordinate2D coord; | |
coord.latitude = newLocation.coordinate.latitude; | |
coord.longitude = newLocation.coordinate.longitude; | |
//do stuff with location | |
} | |
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error | |
{ | |
if(error.code == kCLErrorDenied) //user has denied the App to use location | |
{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"Please Enable Location Services" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alert show]; | |
} | |
else if(error.code == kCLErrorLocationUnknown) //no location could be found | |
{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Unknown" message:@"Sorry, we could not get your location. Please Enable Location Services." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alert show]; | |
} | |
else | |
{ | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Could Not Get Your Location" message:[error description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; | |
[alert show]; | |
} | |
//stop polling for location | |
[self.locationManager stopUpdatingLocation]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment