iOS10通知及通知拓展Extension使用详解

来源:互联网 发布:新东方英语软件下载 编辑:程序博客网 时间:2024/04/28 12:02


1.1-iOS10拓展简介

  • iOS10系统最大的一个亮点就是增加了系统应用的拓展功能Extension

    • Extension功能可以理解为自定义系统界面
  • 本小节我们就以自定义系统通知界面来学习一下Extension的使用

    • 其他功能的Extension我们不可能逐一讲解,希望大家能够在理解的基础上,做到举一反三

这里写图片描述


1.2-iOS10通知使用

  • iOS10之后,为了对自定义通知界面拓展Notification Content的支持,iOS系统推出了新的框架<UserNotifications>

    • 通知的使用思路和步骤不变,只是API发生了变化,并且系统全部会有提示,我们只需要根据系统提示修改一下即可
  • 1.请求授权及添加分类

#import "AppDelegate.h"//iOS10通知新框架#import <UserNotifications/UserNotifications.h>//iOS10 自定义通知界面#import <UserNotificationsUI/UserNotificationsUI.h>@interface AppDelegate ()<UNUserNotificationCenterDelegate>@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    //申请授权    //1.创建通知中心    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];    //设置通知中心的代理(iOS10之后监听通知的接收时间和交互按钮的响应是通过代理来完成的)    center.delegate = self;    //2.通知中心设置分类    [center setNotificationCategories:[NSSet setWithObjects:[self createCatrgory], nil]];    //3.请求授权    /**UNAuthorizationOption     UNAuthorizationOptionBadge   = (1 << 0),红色圆圈     UNAuthorizationOptionSound   = (1 << 1),声音     UNAuthorizationOptionAlert   = (1 << 2),内容     UNAuthorizationOptionCarPlay = (1 << 3),车载通知     */    [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {        if (granted == YES) {            NSLog(@"授权成功");        }    }];    return YES;}//当APP处于前台的时候接收到通知- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{    //弹出一个网页    UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 400, 500)];    webview.center = self.window.center;    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.itheima.com"]]];    [self.window addSubview:webview];    //弹出动画    webview.alpha = 0;    [UIView animateWithDuration:1 animations:^{        webview.alpha = 1;    }];}#pragma mark - 创建通知分类(交互按钮)- (UNNotificationCategory *)createCatrgory{    //文本交互(iOS10之后支持对通知的文本交互)    /**options     UNNotificationActionOptionAuthenticationRequired  用于文本     UNNotificationActionOptionForeground  前台模式,进入APP     UNNotificationActionOptionDestructive  销毁模式,不进入APP     */    UNTextInputNotificationAction *textInputAction = [UNTextInputNotificationAction actionWithIdentifier:@"textInputAction" title:@"请输入信息" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"输入" textInputPlaceholder:@"还有多少话要说……"];    //打开应用按钮    UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"foreGround" title:@"打开" options:UNNotificationActionOptionForeground];    //不打开应用按钮    UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"backGround" title:@"关闭" options:UNNotificationActionOptionDestructive];    //创建分类    /**     Identifier:分类的标识符,通知可以添加不同类型的分类交互按钮     actions:交互按钮     intentIdentifiers:分类内部标识符  没什么用 一般为空就行     options:通知的参数   UNNotificationCategoryOptionCustomDismissAction:自定义交互按钮   UNNotificationCategoryOptionAllowInCarPlay:车载交互     */    UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"category" actions:@[textInputAction,action1,action2] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];    return category;}//按钮点击事件- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{    //根据identifer判断按钮类型,如果是textInput则获取输入的文字    if ([response.actionIdentifier isEqualToString:@"textInputAction"]) {        //获取文本响应        UNTextInputNotificationResponse *textResponse = (UNTextInputNotificationResponse *)response;        NSLog(@"输入的内容为:%@",textResponse.userText);    }    //处理其他时间    NSLog(@"%@",response.actionIdentifier);}- (void)applicationWillResignActive:(UIApplication *)application {    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application {    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application {    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application {    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 2.发送通知(含分类交互按钮)
#pragma mark - 发送本地通知- (IBAction)sendLocalNotification:(id)sender {    //1.创建通知中心    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];    //2.检查当前用户授权    [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {        NSLog(@"当前授权状态:%zd",[settings authorizationStatus]);        //3.创建通知        UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init];        //3.1通知标题        notification.title = [NSString localizedUserNotificationStringForKey:@"传智播客" arguments:nil];        //3.2小标题        notification.subtitle = @"hellow world";        //3.3通知内容        notification.body = @"欢迎来到黑马程序员";        //3.4通知声音        notification.sound = [UNNotificationSound defaultSound];        //3.5通知小圆圈数量        notification.badge = @2;        //4.创建触发器(相当于iOS9中通知触发的时间)        /**通知触发器主要有三种         UNTimeIntervalNotificationTrigger  指定时间触发         UNCalendarNotificationTrigger  指定日历时间触发         UNLocationNotificationTrigger 指定区域触发         */        UNTimeIntervalNotificationTrigger * timeTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];        //5.指定通知的分类  (1)identifer表示创建分类时的唯一标识符  (2)该代码一定要在创建通知请求之前设置,否则无效        notification.categoryIdentifier = @"category";        //给通知添加附件(图片 音乐 电影都可以)        NSString *path = [[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"];        UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"image" URL:[NSURL fileURLWithPath:path] options:nil error:nil];        notification.attachments = @[attachment];        //7.创建通知请求        /**         Identifier:通知请求标识符,用于删除或者查找通知         content:通知的内容         trigger:通知触发器         */        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"localNotification" content:notification trigger:timeTrigger];        //8.通知中心发送通知请求        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {            if (error == nil) {                NSLog(@"通知发送成功");            }            else            {                NSLog(@"%@",error);            }        }];    }];}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 3.通知的移除
#pragma mark - 移除所有通知- (IBAction)removeAllNotification:(id)sender {    //1.创建通知中心    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];    //2.删除已经推送过得通知    [center removeAllDeliveredNotifications];    //3.删除未推送的通知请求    [center removeAllPendingNotificationRequests];}#pragma mark - 移除指定通知- (IBAction)removeSingleNotification:(id)sender {    //1.创建通知中心    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];    //2.删除指定identifer的已经发送的通知    [center removeDeliveredNotificationsWithIdentifiers:@[@"localNotification"]];    //3.删除指定identifer未发送的同志请求    [center removeDeliveredNotificationsWithIdentifiers:@[@"localNotification"]];}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27


1.3-iOS10通知拓展Extension使用

  • 1.添加通知拓展

这里写图片描述

  • 2.通知的拓展Extension实际上相当于在当前的应用程序重新添加一个应用程序,工程会添加对应的代码文件夹和target

这里写图片描述

  • 3.默认拓展控制器只有一个Label,我们可以在这里自定义我们的控制器界面

这里写图片描述

  • 4.也可以加载通知中推送的数据

这里写图片描述

  • 5.配置plist文件 
    • 默认情况下应用程序是不会加载拓展界面的,需要配置plist文件,关闭系统默认通知界面

这里写图片描述

  • 6.运行 
    • 运行的话不需要选择Extension的target,直接选择应用程序的target即可

这里写图片描述


1.4-效果演示

这里写图片描述

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 有人拿着吃完的东西退货怎么办 婴儿个子长的慢怎么办 儿童长得太快怎么办 宝宝个子长太快怎么办 孩子九个月奶水不够怎么办 小孩起热痱子痒怎么办 媳妇生完小孩奶水出不来怎么办 生完孩子下奶疼怎么办 生完小孩没奶水怎么办 孩子半个月奶水越来越少怎么办 半个月后奶水越来越少怎么办 坐月子半个月奶水越来越少怎么办 孩子七个月奶水越来越少怎么办 生完孩子奶水越来越少怎么办 生完孩子回奶了怎么办 产妇3天没奶水怎么办 产后7天了奶水少怎么办 刚生完小孩没有奶水怎么办 突然就没奶水了怎么办 生完小孩没有奶水怎么办 生完宝宝没有奶怎么办 剖腹产奶涨的疼怎么办 生产一天了没奶怎么办 第一天断奶 奶水一直流出怎么办 新生儿刚出生没奶水怎么办 安卓手机死机了怎么办 婴幼儿几天不拉大便怎么办 樱桃吃多了胃不舒服怎么办 空腹吃水果胃不舒服怎么办 吃水果后胃不舒服怎么办 吃水果伤胃了怎么办 大人吃退烧药不出汗怎么办 稍微吃点凉水果胃就疼怎么办 1岁宝宝感冒发烧怎么办 3岁小儿反复发烧怎么办 热感冒喉咙疼要怎么办 孩子感冒咳嗽嗓子疼怎么办 孕妇感冒了嗓子疼咳嗽怎么办 4岁宝宝反复高烧怎么办 小孩吃完药不退烧怎么办 六个月婴儿发烧怎么办退烧快点