//
// ViewController.m
// MonitorRegion
//
// Created by hq on 16/5/17.
// Copyright 2016年 hanqing. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property(nonatomic,strong) CLLocationManager *clm;
@end
@implementation ViewController
-(CLLocationManager *)clm{
if (_clm==nil) {
_clm=[[CLLocationManager alloc]init];
_clm.delegate=self;
if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {
[_clm requestAlwaysAuthorization];
}
}
return _clm;
}
- (void)viewDidLoad {
[super viewDidLoad];
//设置中心点,传经纬度过去
CLLocationCoordinate2D center=CLLocationCoordinate2DMake(37.785834, -122.406417);
//设置监听的区域
CLCircularRegion *region=[[CLCircularRegion alloc] initWithCenter:center radius:1000 identifier:@"hq"];
//开始监听
[self.clm startMonitoringForRegion:region];
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
NSLog(@"%@进入该区域",region.identifier);
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
NSLog(@"%@离开该区域",region.identifier);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
监听用户进入,离开某个区域,需要请求获取用户的位置,如果想在后台也能监听,需要配置ios9后台监听
最新推荐文章于 2022-11-02 19:09:57 发布