#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *manager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
if ([CLLocationManager locationServicesEnabled] == NO) {
return;
}
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
[self.manager requestAlwaysAuthorization];
} else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
}
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(37.785834, 122.406417);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:500 identifier:@"RegionIdentifier"];
[self.manager startMonitoringForRegion:region];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if (status == kCLAuthorizationStatusAuthorizedAlways) {
NSLog(@"授权成功");
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
switch (error.code) {
case kCLErrorLocationUnknown:
NSLog(@"位置未知");
break;
case kCLErrorDenied:
NSLog(@"授权拒绝");
break;
default:
break;
}
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"进入了监听: %@", region.identifier);
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"离开了监听: %@", region.identifier);
}
#pragma mark - Getter & Setter
- (CLLocationManager *)manager
{
if (_manager == nil) {
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;
}
return _manager;
}
@end