iOS 区域监听
2017-10-27 15:02
120 查看
iOS_成才录 关注
2015.11.12 19:31* 字数 207 阅读 566评论
4喜欢 6
一、基本步骤
1.导入CoreLocation框架和对应的主头文件#import <CoreLocation/CoreLocation.h>
2.创建CLLcationManager对象,并设置代理,请求授权(iOS8.0之后才需要),请求前后台定位授权,并配置KEY
pragma mark -懒加载
-(CLLocationManager *)locationM{
if (!_locationM) {
_locationM = [[CLLocationManager alloc] init];
_locationM.delegate = self;
// 主动请求定位授权 if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) [_locationM requestAlwaysAuthorization]; } return _locationM;
}
+ 3.调用CLLcationManager对象的startMonitoringForRegion:方法进行监听指定区域 ```objc // 0. 创建一个区域 // 1.确定区域中心 CLLocationCoordinate2D center = CLLocationCoordinate2DMake(101.123, 20.345); // 确定区域半径 CLLocationDistance radius = 1000; // 使用前必须判定当前的监听区域半径是否大于最大可被监听的区域半径 if(radius > self.locationM.maximumRegionMonitoringDistance) { radius = self.locationM.maximumRegionMonitoringDistance; } CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:radius identifier:@"JP"]; // 区域监听 // [self.locationM startMonitoringFo d97d rRegion:region]; // 请求区域状态(如果发生了进入或者离开区域的动作也会调用对应的代理方法) [self.locationM requestStateForRegion:region];
4.实现代理方法,获取区域进入或者离开等状态
#pragma mark -CLLocationManagerDelegate /** * 进入区域调用(是一个动作) * * @param manager 位置管理者 * @param region 区域 */ -(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { NSLog(@"进入区域"); self.noticeLabel.text = @"进入区域"; } /** * 离开某个区域调用 * * @param manager 位置管理者 * @param region 区域 */ -(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { NSLog(@"离开区域"); self.noticeLabel.text = @" 离开某个区域"; } /** * 请求某个区域的状态是调用 * * @param manager 位置管理者 * @param state 状态 * @param region 区域 */ -(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region { // CLRegionStateUnknown, 未知状态 // CLRegionStateInside, // 在区域内部 // CLRegionStateOutside // 区域外面 if(state == CLRegionStateInside) { self.noticeLabel.text = @"在区域内部"; }else if (state == CLRegionStateOutside) { self.noticeLabel.text = @"区域外面"; } } /** * 监听区域失败时调用 * * @param manager 位置管理者 * @param region 区域 * @param error 错误 */ -(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error { // 经验: 一般在这里, 做移除最远的区域 // [manager stopMonitoringForRegion:最远区域] }
二、注意
1.使用前先判断区域监听是否可用2.注意监听区域的个数 (区域监听个数有上限)
3.注意区域半径是否大于最大监听半径
三、应用场景
1.结合推送通知使用,可以当用户进入到某个区域内(例如肯德基)之后,向用户推送优惠卡等信息
四、实现代码
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> /** 位置管理者 */ @property (nonatomic, strong) CLLocationManager *locationM; @property (weak, nonatomic) IBOutlet UILabel *noticeLabel; @end @implementation ViewController #pragma mark -懒加载 -(CLLocationManager *)locationM { if (!_locationM) { _locationM = [[CLLocationManager alloc] init]; _locationM.delegate = self; // 主动请求定位授权 if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) [_locationM requestAlwaysAuthorization]; } return _locationM; } - (void)viewDidLoad { /** 如果想要区域监听, 必须获取用户的定位授权 */ // 监听的区域个数有限制 // 判定某个区域对应的类, 能否被监听 if([CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) { // 0. 创建一个区域 // 1.确定区域中心 CLLocationCoordinate2D center = CLLocationCoordinate2DMake(101.123, 20.345); // 确定区域半径 CLLocationDistance radius = 1000; // 使用前必须判定当前的监听区域半径是否大于最大可被监听的区域半径 if(radius > self.locationM.maximumRegionMonitoringDistance) { radius = self.locationM.maximumRegionMonitoringDistance; } CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:radius identifier:@"JP"]; // 区域监听 // [self.locationM startMonitoringForRegion:region]; // 请求区域状态(如果发生了进入或者离开区域的动作也会调用对应的代理方法) [self.locationM requestStateForRegion:region]; } } #pragma mark -CLLocationManagerDelegate /** * 进入区域调用(是一个动作) * * @param manager 位置管理者 * @param region 区域 */ -(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { NSLog(@"进入区域"); self.noticeLabel.text = @"进入区域"; } /** * 离开某个区域调用 * * @param manager 位置管理者 * @param region 区域 */ -(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { NSLog(@"离开区域"); self.noticeLabel.text = @"离开区域"; } /** * 请求某个区域的状态是调用 * * @param manager 位置管理者 * @param state 状态 * @param region 区域 */ -(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region { // CLRegionStateUnknown, 未知状态 // CLRegionStateInside, // 在区域内部 // CLRegionStateOutside // 区域外面 if(state == CLRegionStateInside) { self.noticeLabel.text = @"在区域内部"; }else if (state == CLRegionStateOutside) { self.noticeLabel.text = @"区域外面"; } } /** * 监听区域失败时调用 * * @param manager 位置管理者 * @param region 区域 * @param error 错误 */ -(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error { // 经验: 一般在这里, 做移除最远的区域 // [manager stopMonitoringForRegion:最远区域] } @end
相关文章推荐
- iOS地图 -- 区域监听的实现和小练习
- 【iOS】7.4 定位服务->2.1.3.3 定位 - 官方框架CoreLocation 功能3:区域监听
- ios 定位 监听是否跨入某个指定的区域
- 【iOS】7.4 定位服务->2.1.3.3 定位 - 官方框架CoreLocation 功能3:区域监听
- iOS 监听App音量的变化(转)
- 解决 iOS View Controller Push/Pop 时 右上角部分区域的黑影
- IOS二维码识别有效区域偏移问题解决方案
- 【iOS-Cocos2d游戏开发之五】【2】多触点与触屏事件详解(单一监听、事件分发)
- iOS 监听键盘实现页面上滑下滑方法
- iOS学习之动态添加Button和监听UIAlertView按钮
- IOS 监听slider滑动
- ios监听程序回到前台,viewController获取事件
- 38.微信二维码扫描效果--设定扫描区域,周边是半透明--iOS原生二维码扫描
- IOS中的几中观察监听模式
- ios观察者设计模式。(在一个类监听另一个类属性的变化时响应)
- ios开发——日常之可用通知监听的系统事件
- IOS之监听UITextField的编辑事件
- iOS开发之-------监听textfield编辑状态
- 【快讯】iOS曝严重安全漏洞:iPhone用户或已被监听数年
- iOS 监听网络连接状态之AFN