3D touch 静态、动态设置及进入APP的跳转方式

申明Quick Action有两种方式:静态和动态

静态是在info.plist文件中申明,动态则是在代码中注册,系统支持两者同时存在。

-系统限制每个app最多显示4个快捷图标,包括静态和动态

静态

在app的plist文件中增加如下申明:

<key>UIApplicationShortcutItems</key>
<array>
<dict>

    <key>UIApplicationShortcutItemIconType</key>
    <string>UIApplicationShortcutIconTypeSearch</string>

    <key>UIApplicationShortcutItemSubtitle</key>
    <string>shortcutSubtitle1</string>

    <key>UIApplicationShortcutItemTitle</key>
    <string>shortcutTitle1</string>

    <key>UIApplicationShortcutItemType</key>
    <string>First</string>

    <key>UIApplicationShortcutItemUserInfo</key>
    <dict>
    <key>firstShorcutKey1</key>
    <string>firstShortcutKeyValue1</string>
   </dict>

</dict>
</array>


UIApplicationShortcutItemType:其实是快捷方式的id,唯一标签

UIApplicationShortcutItemTitle:标题

UIApplicationShortcutItemSubtitle:副标题

UIApplicationShortcutItemIconType:图标

UIApplicationShortcutItemIconFile: 自定义图标,上面IconType定义将被忽略。图标格式35x35像素单色。

UIApplicationShortcutItemUserInfo:额外信息

系统自带图标的样式分别是:

UIApplicationShortcutIconTypeCompose,

    UIApplicationShortcutIconTypePlay,

    UIApplicationShortcutIconTypePause,

    UIApplicationShortcutIconTypeAdd,

    UIApplicationShortcutIconTypeLocation,

    UIApplicationShortcutIconTypeSearch,

    UIApplicationShortcutIconTypeShare,

    UIApplicationShortcutIconTypeProhibit       NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeContact        NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeHome           NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeMarkLocation   NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeFavorite       NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeLove           NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeCloud          NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeInvitation     NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeConfirmation   NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeMail           NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeMessage        NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeDate           NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeTime           NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeCapturePhoto   NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeCaptureVideo   NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeTask           NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeTaskCompleted  NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeAlarm          NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeBookmark       NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeShuffle        NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeAudio          NS_ENUM_AVAILABLE_IOS(9_1),

    UIApplicationShortcutIconTypeUpdate         NS_ENUM_AVAILABLE_IOS(9_1)

NS_ENUM_AVAILABLE_IOS(9_1)  有这个的是在9.1 系统后才支持的,注意下哈。

 

 

上面的看不懂,没关系,照着下面的图打,就行。
我打的时候,没提示,打错一个字母就没效果。
实际效果:
 
 

 

 

动态

UIApplication对象多了一个支持快捷方式的数组

@property(nonatomic, copy) NSArray <UIApplicationShortcutItem *> *shortcutItems


如果需要增加快捷方式,可以赋值给shortcutItems属性。

如果静态和动态方式同时使用的话,赋值的时候也不会覆盖静态方式申明的快捷方式,不用担心。

UIApplicationShortcutItem对象init方法

(instancetype)initWithType:(NSString *)type 
      localizedTitle:(NSString *)localizedTitle
   localizedSubtitle:(NSString *)localizedSubtitle
                icon:(UIApplicationShortcutIcon *)icon
            userInfo:(NSDictionary *)userInfo`


各个字段跟上面静态方式申明的时候意义一样。

其中定义快捷方式图标UIApplicationShortcutIcon,有3种init方法:

`

// 根据系统预置图标类型
-(instancetype)iconWithType:(UIApplicationShortcutIconType)type

// 根据app bundle里面的图标名称自定义
-(instancetype)iconWithTemplateImageName:(NSString *)templateImageName

// 根据通讯录联系人名称
-(instancetype)iconWithContact:(CNContact *)contact`



// 下面demo, 可以直接复制进入application.m 里使用
//快捷方式响应方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED{
/*   //这是我的demo里面的控制器 跳转方法, 原理就是根据shortcutItem.type 判断跳转页面
 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController *mainView = [storyboard instantiateViewControllerWithIdentifier:@"mainController"];
    UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:mainView];
    self.window.rootViewController = mainNav;
    [self.window makeKeyAndVisible];
   
    //判断先前我们设置的快捷选项标签唯一标识,根据不同标识执行不同操作
    if([shortcutItem.type isEqualToString:@"Video"  ]){
        VideoViewController *videoVC= [VideoViewController new];
        [self.window.rootViewController presentViewController:videoVC animated:YES completion:^{
        }];
    } else if ([shortcutItem.type isEqualToString:@"PIC"]) {
        PICViewController *picVC = [PICViewController new];
        [mainNav pushViewController:picVC animated:NO];
    } else if ([shortcutItem.type isEqualToString:@"NEW"]) {
        NewViewController *newVC = [NewViewController new];
        [mainNav pushViewController:newVC animated:NO];
    }


    if (completionHandler) {
        completionHandler(YES);
    }
   */
   
   // 打印userInfo (传值用)  type(这是快捷方式的唯一标签,根据它的返回值进行判断,在执行需要的操作)
    NSLog(@"userInfo %@ type %@",shortcutItem.userInfo,shortcutItem.type);
   
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //代码的方式添加快捷方式 动态
    if (application.shortcutItems.count==0) {
        //
        UIMutableApplicationShortcutItem *shortItem1=[[UIMutableApplicationShortcutItem alloc] initWithType:@"UIApplicationShortcutIconTypePlay" localizedTitle:@"段子" localizedSubtitle:@"进入段子" icon:[UIApplicationShortcutIcon iconWithType: UIApplicationShortcutIconTypePlay]userInfo:@{@"firstShortcutKey1":@"fristShorcut"}];
        UIMutableApplicationShortcutItem *shortItem2=[[UIMutableApplicationShortcutItem alloc] initWithType:@"UIApplicationShortcutIconTypePlay" localizedTitle:@"视频" localizedSubtitle:@"进入视频" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePause] userInfo:@{@"firstShortcutKey1":@"secondShorcut"}];
         UIMutableApplicationShortcutItem *shortItem3=[[UIMutableApplicationShortcutItem alloc] initWithType:@"UIApplicationShortcutIconTypePlay" localizedTitle:@"图片" localizedSubtitle:@"进入图片" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePause] userInfo:@{@"firstShortcutKey1":@"thridShorcut"}];
         UIMutableApplicationShortcutItem *shortItem4=[[UIMutableApplicationShortcutItem alloc] initWithType:@"UIApplicationShortcutIconTypePlay" localizedTitle:@"资讯" localizedSubtitle:@"进入资讯" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePause] userInfo:@{@"firstShortcutKey1":@"fiveShorcut"}];
       
        application.shortcutItems=@[shortItem1,shortItem2,shortItem3,shortItem4];
       
    }
    return YES;
}
 
测试效果如下:
 最后一个小点: APP 图标在屏幕的哪侧,快捷方式就在哪侧。
 右边

左边:

原文地址:https://www.cnblogs.com/DafaRan/p/5949169.html