iOS 區(qū)域監(jiān)聽

一藤违、基本步驟

  • 1.導(dǎo)入CoreLocation框架和對應(yīng)的主頭文件

     #import <CoreLocation/CoreLocation.h>
    
  • 2.創(chuàng)建CLLcationManager對象,并設(shè)置代理,請求授權(quán)(iOS8.0之后才需要)憔儿,請求前后臺定位授權(quán),并配置KEY

pragma mark -懶加載

-(CLLocationManager *)locationM
{
if (!_locationM) {
_locationM = [[CLLocationManager alloc] init];
_locationM.delegate = self;

    // 主動請求定位授權(quán)
    if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
    [_locationM requestAlwaysAuthorization];
}
return _locationM;

}

+ 3.調(diào)用CLLcationManager對象的startMonitoringForRegion:方法進行監(jiān)聽指定區(qū)域

```objc
     // 0. 創(chuàng)建一個區(qū)域
     // 1.確定區(qū)域中心
     CLLocationCoordinate2D center = CLLocationCoordinate2DMake(101.123, 20.345);
     // 確定區(qū)域半徑
     CLLocationDistance radius = 1000;
     
     // 使用前必須判定當(dāng)前的監(jiān)聽區(qū)域半徑是否大于最大可被監(jiān)聽的區(qū)域半徑
     if(radius > self.locationM.maximumRegionMonitoringDistance)
     {
         radius = self.locationM.maximumRegionMonitoringDistance;
     }
     
     CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:radius identifier:@"JP"];
     // 區(qū)域監(jiān)聽
     //    [self.locationM startMonitoringForRegion:region];
     
     // 請求區(qū)域狀態(tài)(如果發(fā)生了進入或者離開區(qū)域的動作也會調(diào)用對應(yīng)的代理方法)
     [self.locationM requestStateForRegion:region];
  • 4.實現(xiàn)代理方法,獲取區(qū)域進入或者離開等狀態(tài)
#pragma mark -CLLocationManagerDelegate
/**
 *  進入?yún)^(qū)域調(diào)用(是一個動作)
 *
 *  @param manager 位置管理者
 *  @param region  區(qū)域
 */
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"進入?yún)^(qū)域");
    self.noticeLabel.text = @"進入?yún)^(qū)域";
}

/**
 *  離開某個區(qū)域調(diào)用
 *
 *  @param manager 位置管理者
 *  @param region  區(qū)域
 */
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
     NSLog(@"離開區(qū)域");
      self.noticeLabel.text = @" 離開某個區(qū)域";
}


/**
 *  請求某個區(qū)域的狀態(tài)是調(diào)用
 *
 *  @param manager 位置管理者
 *  @param state   狀態(tài)
 *  @param region  區(qū)域
 */
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
//    CLRegionStateUnknown,   未知狀態(tài)
//    CLRegionStateInside, // 在區(qū)域內(nèi)部
//    CLRegionStateOutside // 區(qū)域外面
    if(state == CLRegionStateInside)
    {
        self.noticeLabel.text = @"在區(qū)域內(nèi)部";
    }else if (state == CLRegionStateOutside)
    {
         self.noticeLabel.text = @"區(qū)域外面";
    }
}

/**
 *  監(jiān)聽區(qū)域失敗時調(diào)用
 *
 *  @param manager 位置管理者
 *  @param region  區(qū)域
 *  @param error   錯誤
 */
-(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
    // 經(jīng)驗: 一般在這里, 做移除最遠的區(qū)域
//    [manager stopMonitoringForRegion:最遠區(qū)域]
}

二、注意

  • 1.使用前先判斷區(qū)域監(jiān)聽是否可用
  • 2.注意監(jiān)聽區(qū)域的個數(shù) (區(qū)域監(jiān)聽個數(shù)有上限)
  • 3.注意區(qū)域半徑是否大于最大監(jiān)聽半徑

三、應(yīng)用場景

  • 1.結(jié)合推送通知使用,可以當(dāng)用戶進入到某個區(qū)域內(nèi)(例如肯德基)之后,向用戶推送優(yōu)惠卡等信息

四夯膀、實現(xiàn)代碼

#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;
        
        // 主動請求定位授權(quán)
        if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
        [_locationM requestAlwaysAuthorization];
    }
    return _locationM;
}


- (void)viewDidLoad {
    
    /** 如果想要區(qū)域監(jiān)聽, 必須獲取用戶的定位授權(quán) */

    // 監(jiān)聽的區(qū)域個數(shù)有限制
    
    // 判定某個區(qū)域?qū)?yīng)的類, 能否被監(jiān)聽
    if([CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]])
    {
        // 0. 創(chuàng)建一個區(qū)域
        // 1.確定區(qū)域中心
        CLLocationCoordinate2D center = CLLocationCoordinate2DMake(101.123, 20.345);
        // 確定區(qū)域半徑
        CLLocationDistance radius = 1000;
        
        // 使用前必須判定當(dāng)前的監(jiān)聽區(qū)域半徑是否大于最大可被監(jiān)聽的區(qū)域半徑
        if(radius > self.locationM.maximumRegionMonitoringDistance)
        {
            radius = self.locationM.maximumRegionMonitoringDistance;
        }
        
        CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:radius identifier:@"JP"];
        // 區(qū)域監(jiān)聽
        //    [self.locationM startMonitoringForRegion:region];
        
        // 請求區(qū)域狀態(tài)(如果發(fā)生了進入或者離開區(qū)域的動作也會調(diào)用對應(yīng)的代理方法)
        [self.locationM requestStateForRegion:region];
    }
}

#pragma mark -CLLocationManagerDelegate
/**
 *  進入?yún)^(qū)域調(diào)用(是一個動作)
 *
 *  @param manager 位置管理者
 *  @param region  區(qū)域
 */
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"進入?yún)^(qū)域");
    self.noticeLabel.text = @"進入?yún)^(qū)域";
}

/**
 *  離開某個區(qū)域調(diào)用
 *
 *  @param manager 位置管理者
 *  @param region  區(qū)域
 */
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    NSLog(@"離開區(qū)域");
      self.noticeLabel.text = @"離開區(qū)域";
}


/**
 *  請求某個區(qū)域的狀態(tài)是調(diào)用
 *
 *  @param manager 位置管理者
 *  @param state   狀態(tài)
 *  @param region  區(qū)域
 */
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
//    CLRegionStateUnknown,   未知狀態(tài)
//    CLRegionStateInside, // 在區(qū)域內(nèi)部
//    CLRegionStateOutside // 區(qū)域外面
    if(state == CLRegionStateInside)
    {
        self.noticeLabel.text = @"在區(qū)域內(nèi)部";
    }else if (state == CLRegionStateOutside)
    {
         self.noticeLabel.text = @"區(qū)域外面";
    }
}

/**
 *  監(jiān)聽區(qū)域失敗時調(diào)用
 *
 *  @param manager 位置管理者
 *  @param region  區(qū)域
 *  @param error   錯誤
 */
-(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
    // 經(jīng)驗: 一般在這里, 做移除最遠的區(qū)域
//    [manager stopMonitoringForRegion:最遠區(qū)域]   
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末旧找,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子吹艇,更是在濱河造成了極大的恐慌惰蜜,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件受神,死亡現(xiàn)場離奇詭異抛猖,居然都是意外死亡,警方通過查閱死者的電腦和手機鼻听,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門财著,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人撑碴,你說我怎么就攤上這事撑教。” “怎么了醉拓?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵驮履,是天一觀的道長鱼辙。 經(jīng)常有香客問我,道長玫镐,這世上最難降的妖魔是什么倒戏? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮恐似,結(jié)果婚禮上杜跷,老公的妹妹穿的比我還像新娘。我一直安慰自己矫夷,他們只是感情好葛闷,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著双藕,像睡著了一般淑趾。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上忧陪,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天扣泊,我揣著相機與錄音,去河邊找鬼嘶摊。 笑死延蟹,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的叶堆。 我是一名探鬼主播阱飘,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼虱颗!你這毒婦竟也來了沥匈?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤忘渔,失蹤者是張志新(化名)和其女友劉穎咐熙,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體辨萍,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡棋恼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了锈玉。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片爪飘。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖拉背,靈堂內(nèi)的尸體忽然破棺而出师崎,到底是詐尸還是另有隱情,我是刑警寧澤椅棺,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布犁罩,位于F島的核電站齐蔽,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏床估。R本人自食惡果不足惜含滴,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望丐巫。 院中可真熱鬧谈况,春花似錦、人聲如沸递胧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽缎脾。三九已至祝闻,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間遗菠,已是汗流浹背联喘。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留舷蒲,地道東北人。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓友多,卻偏偏與公主長得像牲平,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子域滥,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容