简介
在支持 3D Touch 的设备上,Quick Actions 可以让用户更快,更少的操作步骤去完成他们最常做的事情,其中这么多操作可以通过主屏幕直接完成。比如用力地长按微信图标,会弹出诸如"扫一扫"和"我的二维码"等常用功能。
支持的设备是在 iPhone 6S 和 6S Plus 及后续出的 iPhone 系列,系统则是从 iOS 9.0 开始支持 3D Touch。
支持两种方式创建,但是不冲突
一、info.plist创建
快捷方式支持在 Info.plist 里直接定义,主键是UIApplicationShortcutItems(Property List查看时显示:Home Screen Shortcut Items),它是一个数组Array。
每一个itme里面的key可下拉查看,type是必填项
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemType</key> //必填
<string>Scan</string>
<key>UIApplicationShortcutItemTitle</key>
<string>扫一扫</string>
</dict>
</array>
从桌面进来后,处理方式都是在appdelegate代理方法里- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
二、代码创建
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
创建
- (void)initQuickActions:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (@available(iOS 9.0, *)) {
//【打卡】快捷方式
UIApplicationShortcutIcon *clockIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLocation];
UIApplicationShortcutItem *clockItem = [[UIApplicationShortcutItem alloc] initWithType:@"NM_Clock" localizedTitle:D_LocalizedString(@"CIO_ClockIn") localizedSubtitle:D_LocalizedString(@"CIO_ClockInOut") icon:clockIcon userInfo:nil];
//【工时】快捷方式
UIApplicationShortcutIcon *timesheetIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"rm_gongshitianbao"];
UIApplicationShortcutItem *timesheetItem = [[UIApplicationShortcutItem alloc] initWithType:@"NM_Timesheet" localizedTitle:D_LocalizedString(@"Timesheet") localizedSubtitle:D_LocalizedString(@"TS_TotalWorkTime") icon:timesheetIcon userInfo:nil];
//【通讯录】快捷方式
UIApplicationShortcutItem *addressItem = [[UIApplicationShortcutItem alloc] initWithType:@"NM_AddressList" localizedTitle:D_LocalizedString(@"Contacts_ModelName") localizedSubtitle:nil icon:nil userInfo:nil];
application.shortcutItems = @[clockItem, timesheetItem, addressItem];
}
}
/// 处理从快捷方式进来后的逻辑
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:@"" forKey:@"docid"];
if ([shortcutItem.type isEqualToString:@"NM_Clock"]) { //打卡
[params setObject:@"Quick_CLOCK" forKey:@"type"];
} else if ([shortcutItem.type isEqualToString:@"NM_Timesheet"]) { //工时
[params setObject:RemoteNoti_timesheetReq forKey:@"type"];
} else if ([shortcutItem.type isEqualToString:@"NM_AddressList"]) { //通讯录
[params setObject:@"Quick_ADDRESSLIST" forKey:@"type"];
}
//此方法就是类似收到通知后的操作,需要做下处理
[self remoteNotification:params.mutableCopy];
}