您的位置:首页 > 移动开发 > IOS开发

监听用户进入,离开某个区域,需要请求获取用户的位置,如果想在后台也能监听,需要配置ios9后台监听

2016-05-18 09:42 369 查看
//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: