iOS10通知及通知拓展Extension使用详解(附Demo)

2024-09-02 15:58

本文主要是介绍iOS10通知及通知拓展Extension使用详解(附Demo),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.1-iOS10拓展简介

1.2-iOS10通知使用

1.3-iOS10通知拓展Extension使用

1.4-效果演示

  • 如果对开发有兴趣的可以来黑马学习iOS开发:黑马程序员

  • 源代码下载地址:Deme下载

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.请求授权/**UNAuthorizationOptionUNAuthorizationOptionBadge   = (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之后支持对通知的文本交互)/**optionsUNNotificationActionOptionAuthenticationRequired  用于文本UNNotificationActionOptionForeground  前台模式,进入APPUNNotificationActionOptionDestructive  销毁模式,不进入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
  • 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);}}];}];
}
  • 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.3-iOS10通知拓展Extension使用

  • 1.添加通知拓展

这里写图片描述

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

这里写图片描述

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

这里写图片描述

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

这里写图片描述

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

这里写图片描述

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

这里写图片描述

1.4-效果演示

这里写图片描述

这篇关于iOS10通知及通知拓展Extension使用详解(附Demo)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1130411

相关文章

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin

Linux之计划任务和调度命令at/cron详解

《Linux之计划任务和调度命令at/cron详解》:本文主要介绍Linux之计划任务和调度命令at/cron的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux计划任务和调度命令at/cron一、计划任务二、命令{at}介绍三、命令语法及功能 :at

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停

如何使用Nginx配置将80端口重定向到443端口

《如何使用Nginx配置将80端口重定向到443端口》这篇文章主要为大家详细介绍了如何将Nginx配置为将HTTP(80端口)请求重定向到HTTPS(443端口),文中的示例代码讲解详细,有需要的小伙... 目录1. 创建或编辑Nginx配置文件2. 配置HTTP重定向到HTTPS3. 配置HTTPS服务器

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

一文详解如何在Python中从字符串中提取部分内容

《一文详解如何在Python中从字符串中提取部分内容》:本文主要介绍如何在Python中从字符串中提取部分内容的相关资料,包括使用正则表达式、Pyparsing库、AST(抽象语法树)、字符串操作... 目录前言解决方案方法一:使用正则表达式方法二:使用 Pyparsing方法三:使用 AST方法四:使用字