/*****监听用户是否进入和走出 在某个区域*****/
1 #import "ViewController.h"
#import <CoreLocation/CoreLocation.h> @interface ViewController ()<CLLocationManagerDelegate> /** */
@property (nonatomic, strong) CLLocationManager *locationM; @property (weak, nonatomic) IBOutlet UILabel *notice;
@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 { // 参数, 必须继承自 clregion
if ([CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) { // 1. 创建区域
// 1. 1 确定区域中心
CLLocationCoordinate2D center = {21.123, 121.345}; // 1.2 区域半径
CLLocationDistance distance = ;
//2128000.000000 最大监控限度
if (distance > self.locationM.maximumRegionMonitoringDistance) {
distance = self.locationM.maximumRegionMonitoringDistance;
} CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:distance identifier:@"一个神秘的地方"]; /**以下两种方式都能监听 是否在区域 里面,建议用监控状态的方式 .下面两种方式都写出来了**/ //-------######-----(方式一 start)------------
54
55 // 2. 监听区域(会调用 didEnterRegion 和 didExitRegion 方法)
56 // [self.locationM startMonitoringForRegion:region];
57 //-------######-----(方式一 end )------------ //-------######-----(方式二 start)------------ // 请求某个区域当前状态(在后台也可以监听到某个区域)
63 /***
64 CLRegionStateUnknown,
65 CLRegionStateInside,
66 CLRegionStateOutside
67 **/
68
69 // 2. 监听状态(会调用 didDetermineState 方法)
70 [self.locationM requestStateForRegion:region]; //------######------(方式二 end)------------ } } #pragma mark - CLLocationManagerDelegate 代理方法 /**
* 进入区域
*
* @param manager 位置管理者
* @param region 区域
*/
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"进入区域---%@", region.identifier);
self.notice.text = @"欢迎来到一个神秘的地方, 给你钱";
} /**
* 离开区域
*
* @param manager 位置管理者
* @param region 区域
*/
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"离开区域---%@", region.identifier);
self.notice.text = @"祝你们有大好前途";
}
/**
* 注册区域失败调用
*
* @param manager 位置管理者
* @param region 区域
* @param error 错误信息
*/
-(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{ // 移除距离较远的region
// [self.locationM stopMonitoringForRegion:region比较远的region ] } /**
* 当我们请求区域状态时调用
*
* @param manager 位置管理者
* @param state 状态
* @param region 区域
*/
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
if(state == CLRegionStateInside)
{
self.notice.text = @"欢迎来到一个神秘的地方, 给你钱";
} if (state == CLRegionStateOutside) {
self.notice.text = @"祝你们有大好前途"; }
} @end

ios 定位 监听是否跨入某个指定的区域的更多相关文章

  1. iOS如何监听弱网?

    场景: iOS中我们可能经常用到监听网络,不过大部分是监听网络的类型,即2G/3G/4G WIFI,是否连接网络,然而测试人员对APP进行测试时候经常会有一个弱网测试,即在弱网环境下对APP进行测试, ...

  2. iOS 实时监听app的网络连接状态

    实际iOS开发中,在网络通信中我们大部分使用第三方(只谈短链),譬如 AFNetworking.ASIHttpRequest(这个停更了,想必现在没多少人用),swift的 Alamofire 等. ...

  3. Android实现短信监听并且转发到指定的手机号,转发后不留痕

    转载:http://blog.csdn.net/swqqcs/article/details/7252419 通过这些代码也可以对远程手机实现短信控制.有兴趣的可以自己改一下,说一下简单的原理,要实现 ...

  4. iOS,监听tableVIew的偏移量

    1. 添加监听 [self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObs ...

  5. iOS 网络监听、判断

    一 网络监听 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary ...

  6. iOS 关于监听手机截图,UIView生成UIImage, UIImage裁剪与压缩的总结

    一.  关于监听手机截图 1. 背景: 发现商品的售价页总是被人转发截图,为了方便用户添加截图分享的小功能 首先要注册用户截屏操作的通知 - (void)viewDidLoad { [super vi ...

  7. ios --键盘监听JYKeyBoardListener

    没有前言,就是一个简单的键盘监听,自动调整输入框的位置不被键盘遮挡 .h // // JYKeyBoardListener.h // // Created by JianF.Sun on 17/9/2 ...

  8. iOS自学-监听按钮点击、提醒框

    //事件监听的问题 CGRect btn2Frame = CGRectMake(100.0, 150.0, 60.0, 44.0); //两种不同的方式创建 UIButton *btn2 = [UIB ...

  9. iOS键盘监听事件

    1.注册键盘通知事件 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; // 键盘将出现事件监听 [center ...

随机推荐

  1. iOS 自定义Actionsheet

    自定义的Actionsheet效果如下 自定义的思路 1.在window上添加两个图层,背景层和功能层,如下图 2.设置背景层的背景色和透明度,并在背景层上添加点击事件 3.将自定义的view添加为功 ...

  2. iOS之UI--辉光动画

    前言:学习来自YouXianMing老师的博客:<辉光UIView的category>以及YouXianMing老师的github源码:< GlowView >    而我个人 ...

  3. 做一些Spring AOP做过的事,封装 jdk动态代理成为一个黑盒子

      怎么使用eclise 抽取方法,请看  利用eclipse 抽取代码片段为方法   抽取完成之后,还需要 ① 将Collection.class换成  target.getClass(),targ ...

  4. 又一个悬而未决的bug被解决

    之所以叫悬而未决,是因为从我第一次见到这个bug,到现在大概已经过了快两年的时间,期间好几次想解决这个问题,但是一直碍于环境和一些技术上的限制,没有解决,直到昨天在一系列的因素作用下,终于解决了这个问 ...

  5. SAM4E单片机之旅——15、触屏输入与SPI通信

    开发板上配了一个电阻触摸屏,它的控制器是ADS7843,使用SPI进行通信.这次实现的功能是通过SPI接口与该控制器交互,获取触摸屏点击的坐标,并显示在LCD上.略为难点的是SPI作为同步时钟的一种, ...

  6. jetbrains DataGrip 导出Excel

    之前一直不知道如何通过DataGrip 导出Excel,导致每次都是用Navcat来做导出,今天研究了下,原来用TSV格式就能复制到Excel表格,具体步骤如下: ------------------ ...

  7. MySQL 中的 FOUND_ROWS() 与 ROW_COUNT() 函数

    移植sql server 的存储过程到mysql中,遇到了sql server中的: IF @@ROWCOUNT < 1 对应到mysql中可以使用 FOUND_ROWS() 函数来替换. 1. ...

  8. npm报错Error: ENOENT, stat 'D:\NodeLearn\node-global'

    最近想试下当前的当红炸子鸡 Nodejs,在安装配置时,发生了下面的错误: C:\nodejs\npmjs\bin>cd .. C:\nodejs\npmjs>cd .. C:\nodej ...

  9. 解读Python发送邮件

    解读Python发送邮件 Python发送邮件需要smtplib和email两个模块.也正是由于我们在实际工作中可以导入这些模块,才使得处理工作中的任务变得更加的简单.今天,就来好好学习一下使用Pyt ...

  10. Tomcat常用的优化技巧

    (1)屏蔽DNS查询 Web应用程序可以通过Web容器提供的getRemoteHost()方法获得访问Web应用客户的IP地址和名称,但是这样会消耗Web容器的资源,并且还需要通过IP地址和DNS服务 ...