/*****监听用户是否进入和走出 在某个区域*****/
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. 网络请求的基本知识《极客学院 --AFNetworking 2.x 网络解析详解--1》学习笔记

    网络请求的基本知识   我们网络请求用的是HTTP请求 Http请求格式:请求的方法,请求头,请求正文 Http请求的Request fields:请求的头部,以及被请求头部的一些设置 Http请求的 ...

  2. Git基本使用命令

    整理Git的一些基本使用命令.   # 1)克隆代码 boldseas@lian-PC MINGW64 /d/TestGroup $ git clone ssh://git@code.boldseas ...

  3. jquery1.0源码【1-60行】构造函数及全局$变量

    一.jquery源码1-60行 该部分代码主要完成jquery对象的创建,以及全局变量$与jQurey类的映射: /* * jQuery - New Wave Javascript * * Copyr ...

  4. Android 实用代码片段

    一些不常见确又很实用的代码块. 1.精确获取屏幕尺寸(例如:3.5.4.0.5.0寸屏幕) public static double getScreenPhysicalSize(Activity ct ...

  5. 2013MPD上海6.23 PM 光耀:读心术,用户心理的产品之道

    创新的前提是:制度与组织的创新!!!!!!!!!!!!!! 光耀:腾讯互联网业务产品经理(腾讯公司互联网业务系统产品经理.在电子商务.社会化媒体等方面有深入研究.参与腾讯多个重要项目产品工作) 什么是 ...

  6. mysql连接其他表删除某个表的数据

    delete a from TableA a left join TableB b on a.XX = b.YY left join TableC c on c.ZZ = a.XX where 条件

  7. C语言流程控制

    顺序结构 顺序结构是最常用的结构,即从上到下的执行语句. int num=5; num++; num=13; 条件结构 条件结构是当表达式为真的时候执行语句块,C语言提供了两种条件结构 if...el ...

  8. sass+compass+bootstrap三剑合璧高效开发记录

    1. 先搭建环境,下载node.js,rubyinstaller,安装, 安装rubyinstaller时,要选上include system path,这样就会自动将node.js执行添加到wind ...

  9. 05_最长公共子序列问题(LCS)

    问题来源:刘汝佳<算法竞赛入门经典--训练指南> P60 问题7: 问题描述:给两个子序列A和B,求长度最大的公共子序列.比如1,5,2,6,8,和2,3,5,6,9,8,4的最长公共子序 ...

  10. Linux运维式具之pdsh

    做系统运维的时候,我们经常需要同时在多台机器上执行相同的命令,这个时候可以使用类似pssh,pdsh的并行执行shell的工具. 当然,之前在没有使用这些工具之前,如果有ssh无密码互访,我们可以自己 ...