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

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

大家好,又见面了,我是全栈君。

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

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

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

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

      详细操作例如以下:
1.要导入我们须要的类库CoreLocation.framework
     iOS8新特性之基于地理位置的消息通知UILocalNotification

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

CLLocationManager *locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
// request authorization to track the user’s location
[locMan requestWhenInUseAuthorization];

同一时候还要进行配置plist文件
iOS8新特性之基于地理位置的消息通知UILocalNotification
当执行到最后一句时,用户会收到系统提示,同意后app获得授权。

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

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: 就不会被调用。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115683.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java final keyword

    java final keyword

    2021年12月7日
    39
  • 暴走周刊 5.27

    暴走周刊 5.27

    2021年7月7日
    103
  • 完全卸载mysql(亲测有效!!!)「建议收藏」

    完全卸载mysql(亲测有效!!!)「建议收藏」1.停止mysql服务。  “运行”——>“cmd”——>输入“netstopmysql;”    看链接:  https://blog.csdn.net/Ludwig_/article/details/526345872.将控制面板中的mysql正常卸载  控制面板——》“程序”,找到并卸载!3.找到安装路径,将所有关于mysql的文…

    2022年9月30日
    0
  • 【其他】资源整合「建议收藏」

    【其他】资源整合「建议收藏」偶然整理云盘,发现曾经收藏过一些比较不错的资源,正好分享一下;1.C语言教程,郝斌老师作为读书时候的启蒙老师,推荐一波链接:https://pan.baidu.com/s/1rn_cHgNs5qIZV9ON-pcWVw提取码:wa7j2.UI框架链接:https://pan.baidu.com/s/1Q2Bj-i79C1gDWZSvfDVEeQ提取码:a47l3.UI万能框架链接:https://pan.baidu.com/s/1Ikvqo9mtabD104bWVLte2w…

    2022年5月22日
    50
  • 电驴(eMule)下载_电驴链接怎么用

    电驴(eMule)下载_电驴链接怎么用先保证服务器连接。在电骡服务器界面右边的“从URL更新server.met”字样下边的小框里输入“http://upd.emule-security.org/server.met”,然后点击“更新”,下载新的服务器列表即可。Kad连接http://upd.emule-security.org/nodes.dathttp://kademlia.ru/download/nodes.dath…

    2022年10月22日
    0
  • 计算机专业英语词汇1500词

    计算机专业英语词汇1500词计算机专业英语词汇1500词(一)1.filen.文件;v.保存文件2.commandn.命令,指令3.usev.使用,用途4.programn.程序5.linen.(数据,程序)行,线路6.ifconj.如果7.displayvt.显示,显示器8.setv.设置,n.集合9.keyn.键,关键字,关键码10.list…

    2022年6月7日
    34

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号