国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

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

2019-11-09 18:13:39
字體:
供稿:網(wǎng)友

1.1-iOS10拓展簡介

iOS10系統(tǒng)最大的一個亮點就是增加了系統(tǒng)應(yīng)用的拓展功能Extension

Extension功能可以理解為自定義系統(tǒng)界面

本小節(jié)我們就以自定義系統(tǒng)通知界面來學(xué)習(xí)一下Extension的使用

其他功能的Extension我們不可能逐一講解,希望大家能夠在理解的基礎(chǔ)上,做到舉一反三

這里寫圖片描述

1.2-iOS10通知使用

iOS10之后,為了對自定義通知界面拓展Notification Content的支持,iOS系統(tǒng)推出了新的框架<UserNotifications>

通知的使用思路和步驟不變,只是API發(fā)生了變化,并且系統(tǒng)全部會有提示,我們只需要根據(jù)系統(tǒng)提示修改一下即可

1.請求授權(quán)及添加分類

#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. //申請授權(quán) //1.創(chuàng)建通知中心 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; //設(shè)置通知中心的代理(iOS10之后監(jiān)聽通知的接收時間和交互按鈕的響應(yīng)是通過代理來完成的) center.delegate = self; //2.通知中心設(shè)置分類 [center setNotificationCategories:[NSSet setWithObjects:[self createCatrgory], nil]]; //3.請求授權(quán) /**UNAuthorizationOption UNAuthorizationOptionBadge = (1 << 0),紅色圓圈 UNAuthorizationOptionSound = (1 << 1),聲音 UNAuthorizationOptionAlert = (1 << 2),內(nèi)容 UNAuthorizationOptionCarPlay = (1 << 3),車載通知 */ [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted == YES) { NSLog(@"授權(quán)成功"); } }]; return YES;}//當(dāng)APP處于前臺的時候接收到通知- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPResentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ //彈出一個網(wǎng)頁 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 - 創(chuàng)建通知分類(交互按鈕)- (UNNotificationCategory *)createCatrgory{ //文本交互(iOS10之后支持對通知的文本交互) /**options UNNotificationActionOptionAuthenticationRequired 用于文本 UNNotificationActionOptionForeground 前臺模式,進(jìn)入APP UNNotificationActionOptionDestructive 銷毀模式,不進(jìn)入APP */ UNTextInputNotificationAction *textInputAction = [UNTextInputNotificationAction actionWithIdentifier:@"textInputAction" title:@"請輸入信息" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"輸入" textInputPlaceholder:@"還有多少話要說……"]; //打開應(yīng)用按鈕 UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"foreGround" title:@"打開" options:UNNotificationActionOptionForeground]; //不打開應(yīng)用按鈕 UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"backGround" title:@"關(guān)閉" options:UNNotificationActionOptionDestructive]; //創(chuàng)建分類 /** Identifier:分類的標(biāo)識符,通知可以添加不同類型的分類交互按鈕 actions:交互按鈕 intentIdentifiers:分類內(nèi)部標(biāo)識符 沒什么用 一般為空就行 options:通知的參數(shù) 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{ //根據(jù)identifer判斷按鈕類型,如果是textInput則獲取輸入的文字 if ([response.actionIdentifier isEqualToString:@"textInputAction"]) { //獲取文本響應(yīng) UNTextInputNotificationResponse *textResponse = (UNTextInputNotificationResponse *)response; NSLog(@"輸入的內(nèi)容為:%@",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:.}@end1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441452.發(fā)送通知(含分類交互按鈕)#pragma mark - 發(fā)送本地通知- (IBAction)sendLocalNotification:(id)sender { //1.創(chuàng)建通知中心 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; //2.檢查當(dāng)前用戶授權(quán) [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { NSLog(@"當(dāng)前授權(quán)狀態(tài):%zd",[settings authorizationStatus]); //3.創(chuàng)建通知 UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init]; //3.1通知標(biāo)題 notification.title = [NSString localizedUserNotificationStringForKey:@"傳智播客" arguments:nil]; //3.2小標(biāo)題 notification.subtitle = @"hellow world"; //3.3通知內(nèi)容 notification.body = @"歡迎來到黑馬程序員"; //3.4通知聲音 notification.sound = [UNNotificationSound defaultSound]; //3.5通知小圓圈數(shù)量 notification.badge = @2; //4.創(chuàng)建觸發(fā)器(相當(dāng)于iOS9中通知觸發(fā)的時間) /**通知觸發(fā)器主要有三種 UNTimeIntervalNotificationTrigger 指定時間觸發(fā) UNCalendarNotificationTrigger 指定日歷時間觸發(fā) UNLocationNotificationTrigger 指定區(qū)域觸發(fā) */ UNTimeIntervalNotificationTrigger * timeTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; //5.指定通知的分類 (1)identifer表示創(chuàng)建分類時的唯一標(biāo)識符 (2)該代碼一定要在創(chuàng)建通知請求之前設(shè)置,否則無效 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.創(chuàng)建通知請求 /** Identifier:通知請求標(biāo)識符,用于刪除或者查找通知 content:通知的內(nèi)容 trigger:通知觸發(fā)器 */ UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"localNotification" content:notification trigger:timeTrigger]; //8.通知中心發(fā)送通知請求 [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (error == nil) { NSLog(@"通知發(fā)送成功"); } else { NSLog(@"%@",error); } }]; }];}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676812345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667683.通知的移除#pragma mark - 移除所有通知- (IBAction)removeAllNotification:(id)sender { //1.創(chuàng)建通知中心 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; //2.刪除已經(jīng)推送過得通知 [center removeAllDeliveredNotifications]; //3.刪除未推送的通知請求 [center removeAllPendingNotificationRequests];}#pragma mark - 移除指定通知- (IBAction)removeSingleNotification:(id)sender { //1.創(chuàng)建通知中心 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; //2.刪除指定identifer的已經(jīng)發(fā)送的通知 [center removeDeliveredNotificationsWithIdentifiers:@[@"localNotification"]]; //3.刪除指定identifer未發(fā)送的同志請求 [center removeDeliveredNotificationsWithIdentifiers:@[@"localNotification"]];}123456789101112131415161718192021222324252627123456789101112131415161718192021222324252627

1.3-iOS10通知拓展Extension使用

1.添加通知拓展

這里寫圖片描述

2.通知的拓展Extension實際上相當(dāng)于在當(dāng)前的應(yīng)用程序重新添加一個應(yīng)用程序,工程會添加對應(yīng)的代碼文件夾和target

這里寫圖片描述

3.默認(rèn)拓展控制器只有一個Label,我們可以在這里自定義我們的控制器界面

這里寫圖片描述

4.也可以加載通知中推送的數(shù)據(jù)

這里寫圖片描述

5.配置plist文件 默認(rèn)情況下應(yīng)用程序是不會加載拓展界面的,需要配置plist文件,關(guān)閉系統(tǒng)默認(rèn)通知界面

這里寫圖片描述

6.運行 運行的話不需要選擇Extension的target,直接選擇應(yīng)用程序的target即可

這里寫圖片描述

1.4-效果演示

這里寫圖片描述


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 柳州市| 南郑县| 东平县| 台北市| 三台县| 阜康市| 呼伦贝尔市| 峡江县| 建水县| 新乡市| 吉木萨尔县| 林口县| 资阳市| 大城县| 正蓝旗| 浦北县| 宽甸| 白水县| 准格尔旗| 永丰县| 宣城市| 东平县| 新安县| 梁山县| 金坛市| 门头沟区| 原平市| 苗栗市| 凤冈县| 宜丰县| 饶平县| 灵石县| 满城县| 孟州市| 桂林市| 枣强县| 墨玉县| 罗城| 裕民县| 荣昌县| 巨野县|