zoukankan      html  css  js  c++  java
  • iOS8新特性之基于地理位置的消息通知UILocalNotification

        
         苹果在WWDC2014上正式公布了全新的iOS8操作系统。

    界面上iOS8与iOS7相比变化不大,只是在功能方面进行了完好。
                                   
         iOS8中更新和公开了非常多接口,当中有一项本地消息通知UILocalNotification,大家肯定都不陌生。

    可是在iOS8中对其进行了优化和改进。

    如今它能够依据地理位置发起消息通知,即我们在App中设置好一个坐标(经纬度)和半径(范围),当装有本App的设备进入本区域后,App就会发出一个消息通知。
       

          详细操作例如以下:
    1.要导入我们须要的类库CoreLocation.framework
        

    2.登记位置信息,获取用户的授权

    CLLocationManager *locMan = [[CLLocationManager alloc] init];
    locMan.delegate = self;
    // request authorization to track the user’s location
    [locMan requestWhenInUseAuthorization];
    同一时候还要进行配置plist文件

    当执行到最后一句时,用户会收到系统提示,同意后app获得授权。


                                                                              

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

    - (void)locationManager:(CLLocationManager *)manager
        didChangeAuthorizationStatus:(CLAuthorizationStatus)status 
    {
       // check status to see if we’re authorized
       BOOL canUseLocationNotifications =
             (status == kCLAuthorizationStatusAuthorizedWhenInUse);
       if (canUseLocationNotifications) {
           [self startShowingLocationNotifications];
        }
    }
    回调方法里注冊通知
    - (void)startShowingNotifications 
    {
        UILocalNotification *locNotification = [[UILocalNotification alloc]
                                               init];
        locNotification.alertBody = @“You have arrived!”;
        locNotification.regionTriggersOnce = YES;
        locNotification.region = [[CLCircularRegion alloc]
                               initWithCenter:LOC_COORDINATE
                                       radius:LOC_RADIUS
                                   identifier:LOC_IDENTIFIER];
        [[UIApplication sharedApplication]
             scheduleLocalNotification:localNotification];
    }
    //- (instancetype)initWithCenter:(CLLocationCoordinate2D)center //区域的中心 经纬度
    //                            radius:(CLLocationDistance)radius //区域半径   范围
    //                        identifier:(NSString *)identifier;    //通知的唯一标示 描写叙述
    

    4.到了一定区域后触发消息通知。收到消息后app回调方法
    -(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
    {
        NSLog(@"%s",__func__);
    }
    - (void)application:(UIApplication *)application didReceiveLocalNotification:
        (UILocalNotification *)notification
    {
        CLRegion *region = notification.region;
        if (region)
        {
           [self tellFriendsUserArrivedAtRegion:region];
        }
    }
    

    注:
    查看官方文档,了解很多其它UILocalNotification 新增API。
    本服务须要位置信息登记;
    假设位置信息被禁用。这种方法application:didReceiveLocalNotification: 就不会被调用。




  • 相关阅读:
    sublime text2 中Emmet常用的技巧 和快捷键
    JS 常用函数
    javascript 事件模型 及 event对象属性总结
    Plant Ecology Journal Club 分享主题和文献列表, 1-7, 2018年秋
    Perl FASTA文件拆分合并
    筛选特定ID的条目信息
    计算可塑性指数RDPI
    【文献摘抄】2018年9月
    重金属在超富集植物与作物间作系统中的植物吸收和富集
    【R统计】主成分分析2——主成分回归
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5271920.html
Copyright © 2011-2022 走看看