iOS8新特性之基于地理位置的消息通知UILocalNotification

苹果在WWDC2014上正式发布了全新的iOS8操作系统。界面上iOS8与iOS7相比变化不大,不过在功能方面进行了完善。
                                
     iOS8中更新和公开了很多接口,其中有一项本地消息通知UILocalNotification,大家肯定都不陌生。但是在iOS8中对其进行了优化和改进。现在它可以根据地理位置发起消息通知,即我们在App中设置好一个坐标(经纬度)和半径(范围),当装有本App的设备进入本区域后,App就会发出一个消息通知。
   

       具体操作如下:
1.要导入我们需要的类库CoreLocation.framework
     

2.登记位置信息,获取用户的授权
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CLLocationManager *locMan = [[CLLocationManager alloc] init];  
  2. locMan.delegate = self;  
  3. // request authorization to track the user’s location  
  4. [locMan requestWhenInUseAuthorization];  
同时还要进行配置plist文件

当运行到最后一句时,用户会收到系统提示,允许后app获得授权。
                                                                           

3.获取授权后app就会回调方法

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)locationManager:(CLLocationManager *)manager  
  2.     didChangeAuthorizationStatus:(CLAuthorizationStatus)status   
  3. {  
  4.    // check status to see if we’re authorized  
  5.    BOOL canUseLocationNotifications =  
  6.          (status == kCLAuthorizationStatusAuthorizedWhenInUse);  
  7.    if (canUseLocationNotifications) {  
  8.        [self startShowingLocationNotifications];  
  9.     }  
  10. }  
回调方法里注册通知
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (void)startShowingNotifications   
  2. {  
  3.     UILocalNotification *locNotification = [[UILocalNotification alloc]  
  4.                                            init];  
  5.     locNotification.alertBody = @“You have arrived!”;  
  6.     locNotification.regionTriggersOnce = YES;  
  7.     locNotification.region = [[CLCircularRegion alloc]  
  8.                            initWithCenter:LOC_COORDINATE  
  9.                                    radius:LOC_RADIUS  
  10.                                identifier:LOC_IDENTIFIER];  
  11.     [[UIApplication sharedApplication]  
  12.          scheduleLocalNotification:localNotification];  
  13. }  
  14. //- (instancetype)initWithCenter:(CLLocationCoordinate2D)center //区域的中心 经纬度  
  15. //                            radius:(CLLocationDistance)radius //区域半径   范围  
  16. //                        identifier:(NSString *)identifier;    //通知的唯一标示 描述  

4.到了一定区域后触发消息通知,收到消息后app回调方法
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. -(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region  
  2. {  
  3.     NSLog(@"%s",__func__);  
  4. }  
  5. - (void)application:(UIApplication *)application didReceiveLocalNotification:  
  6.     (UILocalNotification *)notification  
  7. {  
  8.     CLRegion *region = notification.region;  
  9.     if (region)  
  10.     {  
  11.        [self tellFriendsUserArrivedAtRegion:region];  
  12.     }  
  13. }  

注:
查看官方文档,了解更多UILocalNotification 新增API;
本服务需要位置信息登记;
如果位置信息被禁用,这个方法application:didReceiveLocalNotification: 就不会被调用;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值