根据公司要去需要长按app有快捷方式打开,搜索了下网上的资料,加上自己的实践,做一个总结。
1.操作须知
3D Touch
1.静态添加 plist添加 :注意key值有变化 直接在源码中复制添加
2.动态添加 代码添加
3.App Store上线会自动添加·分享·
4.图标规格 (推荐图标为一倍大小35*35的单色图片,图片需要背景透明才能显现出图片样式,图标会被渲染成黑色。
[官网](https://developer.apple.com/documentation/uikit/uiapplicationshortcuticontype/uiapplicationshortcuticontypeshare?language=objc)
5.图标分别在上半屏和下半屏的情况下,显示的排序是会不同的,以下半区为标准展示,先展示自定义创建、后展示系统自带,上半屏展示则为镜像展示
A.上线之前菜单中默认有两个,**移除****App** 和 **编辑主屏幕**,上线之后从App Store中下载之后会自动添加一个 **分享****APP**
B.**静态添加** 和 **动态添加** 两种方式可以同时使用, 系统会优先加载静态添加的items, 然后再加载动态添加的items.
C.自定义的菜单 items 数量全部加起来最多只有4个(除掉系统自带的3个,还能添加4个)
D.菜单展示优先级从高到低顺序依次是,静态添加 > 动态添加 > 系统自带,优先级越高,越靠近App图标在设备主屏幕中的位置
2.实践
1.静态添加plist方式
在网上的资料中都会提到一个主键,
UIApplicationShortcutItems,但是在实践的info.plist中无法添加,所以我采用的是直接在info.plist源码里面复制如下。
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemType</key>
<string>SearchAction</string>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeSearch</string>
<key>UIApplicationShortcutItemTitle</key>
<string>搜索</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>搜索你想搜的</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key1</key>
<string>value1</string>
</dict>
</dict>
<dict>
<key>UIApplicationShortcutItemType</key>
<string>ShareAction</string>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeShare</string>
<key>UIApplicationShortcutItemTitle</key>
<string>Share</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>Share an item</string>
<key>UIApplicationShortcutItemUserInfo</key>
<dict>
<key>key2</key>
<string>value2</string>
</dict>
</dict>
</array>
查看图片可以看到没有所谓的主键UIApplicationShortcutItems
2.点击事件监听
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
NSLog(@"name:%@ \n type:%@",shortcutItem.localizedTitle,shortcutItem.type);
UITabBarController *tab = (UITabBarController *)_window.rootViewController;
UINavigationController *nav = tab.viewControllers[tab.selectedIndex];
if ([shortcutItem.type isEqualToString:@"ShareAction"]) {
// 自定义分享
nav.tabBarController.hidesBottomBarWhenPushed=NO;
if (nav.tabBarController.selectedIndex == 2) {
[nav popToRootViewControllerAnimated:YES];
}else {
// 避免回到初始app的tabbar作为根视图的页面还在原来的页面
[nav popToRootViewControllerAnimated:NO];
nav.tabBarController.selectedIndex=2;
}
}else if ([shortcutItem.type isEqualToString:@"SearchAction"]) {
// 搜索
NEPSearchResultViewController *vc = [[NEPSearchResultViewController alloc]init];
vc.searchStr = @"感冒";
[nav pushViewController:vc animated:YES];
}else if ([shortcutItem.type isEqualToString:@"chen"]) {
NSLog(@"识别到了");
}
}
不管是静态添加(plist)或者动态添加(代码)监听事件都是走该方法
performActionForShortcutItem
3.动态添加
#pragma mark - 配置3D Touch
- (void)config3DTouchQuickActionapplication:(UIApplication *)application {
UIApplicationShortcutIcon *searchIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
UIApplicationShortcutItem *search = [[UIApplicationShortcutItem alloc] initWithType:@"search" localizedTitle:@"搜索" localizedSubtitle:nil icon:searchIcon userInfo:nil];
UIApplicationShortcutIcon *publicIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"upload"];
UIApplicationShortcutItem *public = [[UIApplicationShortcutItem alloc] initWithType:@"public" localizedTitle:@"一键发布" localizedSubtitle:nil icon:publicIcon userInfo:nil];
UIApplicationShortcutItem *list = [[UIApplicationShortcutItem alloc] initWithType:@"chen" localizedTitle:@"榜单" localizedSubtitle:@"全区排行" icon:nil userInfo:nil];
application.shortcutItems = @[list, public, search];
}
下半屏和上半屏是排序不一样,互为镜像,如下是下半屏排序(标准),且最多四个,黑色原点表示无图片