/*****监听用户是否进入和走出 在某个区域*****/
1 #import "ViewController.h"
2 #import <CoreLocation/CoreLocation.h>
3
4
5 @interface ViewController ()<CLLocationManagerDelegate>
6
7 /** */
8 @property (nonatomic, strong) CLLocationManager *locationM;
9
10 @property (weak, nonatomic) IBOutlet UILabel *notice;
11 @end
12
13 @implementation ViewController
14
15
16 #pragma mark - 懒加载
17 /** 位置管理者属性的懒加载 */
18 -(CLLocationManager *)locationM
19 {
20 if (!_locationM) {
21 _locationM = [[CLLocationManager alloc] init];
22 _locationM.delegate = self;
23
24 if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
25 [_locationM requestAlwaysAuthorization];
26
27 }
28 return _locationM;
29 }
30
31
32
33 - (void)viewDidLoad {
34
35 // 参数, 必须继承自 clregion
36 if ([CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) {
37
38 // 1. 创建区域
39 // 1. 1 确定区域中心
40 CLLocationCoordinate2D center = {21.123, 121.345};
41
42 // 1.2 区域半径
43 CLLocationDistance distance = 1000;
44 //2128000.000000 最大监控限度
45 if (distance > self.locationM.maximumRegionMonitoringDistance) {
46 distance = self.locationM.maximumRegionMonitoringDistance;
47 }
48
49 CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:distance identifier:@"一个神秘的地方"];
50
51 /**以下两种方式都能监听 是否在区域 里面,建议用监控状态的方式 .下面两种方式都写出来了**/
52
53 //-------######-----(方式一 start)------------
54
55 // 2. 监听区域(会调用 didEnterRegion 和 didExitRegion 方法)
56 // [self.locationM startMonitoringForRegion:region];
57 //-------######-----(方式一 end )------------
58
59
60 //-------######-----(方式二 start)------------
61
62 // 请求某个区域当前状态(在后台也可以监听到某个区域)
63 /***
64 CLRegionStateUnknown,
65 CLRegionStateInside,
66 CLRegionStateOutside
67 **/
68
69 // 2. 监听状态(会调用 didDetermineState 方法)
70 [self.locationM requestStateForRegion:region];
71
72 //------######------(方式二 end)------------
73
74 }
75
76 }
77
78
79 #pragma mark - CLLocationManagerDelegate 代理方法
80
81 /**
82 * 进入区域
83 *
84 * @param manager 位置管理者
85 * @param region 区域
86 */
87 -(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
88 {
89 NSLog(@"进入区域---%@", region.identifier);
90 self.notice.text = @"欢迎来到一个神秘的地方, 给你钱";
91 }
92
93 /**
94 * 离开区域
95 *
96 * @param manager 位置管理者
97 * @param region 区域
98 */
99 -(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
100 {
101 NSLog(@"离开区域---%@", region.identifier);
102 self.notice.text = @"祝你们有大好前途";
103 }
104 /**
105 * 注册区域失败调用
106 *
107 * @param manager 位置管理者
108 * @param region 区域
109 * @param error 错误信息
110 */
111 -(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
112 {
113
114 // 移除距离较远的region
115 // [self.locationM stopMonitoringForRegion:region比较远的region ]
116
117
118 }
119
120 /**
121 * 当我们请求区域状态时调用
122 *
123 * @param manager 位置管理者
124 * @param state 状态
125 * @param region 区域
126 */
127 -(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
128 {
129 if(state == CLRegionStateInside)
130 {
131 self.notice.text = @"欢迎来到一个神秘的地方, 给你钱";
132 }
133
134 if (state == CLRegionStateOutside) {
135 self.notice.text = @"祝你们有大好前途";
136
137 }
138 }
139
140
141
142
143
144
145 @end