iOS- Home Screen Quick Actions

   1.概述: iOS的快捷键分为静态快捷键和动态快捷键,静态快捷键在系统安装时就可以使用,而动态快捷键只有第一次启动程序后能使用。系统限制每个app最多显示4个快捷键,超过四个的action不起作用。而每个快捷键对应一个UIApplicationShortcutItem 对象。

   2.快捷键的创建

   2.1 静态快捷键的创建:在app的info.plist的关键字UIApplicationShortcutItems(value值为数组类型)下,每个Item为一个UIApplicationShortcutItem对象,每一个UIApplicationShortcutItem中能够包含的信息如下:


      UIApplicationShortcutItemType(required)   事件的唯一标识,区分事件                            

      UIApplicationShortcutItemTitle(required) 标题,在无子标题时如果标题太长可自动换行     

   UIApplicationShortcutItemSubtitle     子标题,在标题的下方

   UIApplicationShortcutItemIconType       枚举选取系统中的一个图标

   UIApplicationShortcutItemIconFile   自定义一个图标,以单一颜色单一35*35的大小展示,如果设置这个,UIApplicationShortcutItemIconType将不起作用

   UIApplicationShortcutItemUserInfo   字典,里面可以添加各种key,value。

   2.2动态快捷键的创建:用代码动态添加,UIApplication增加了shortcutItems 属性


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

代码如下:

UIApplicationShortcutIcon *threeIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay];

UIMutableApplicationShortcutItem *threeShortcutItem = [[UIMutableApplicationShortcutItem alloc] initWithType:@"threeType" localizedTitle:@"标题" localizedSubtitle:@"Subtitle" icon:threeIcon userInfo:nil];

UIApplicationShortcutIcon *fourIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePause];

UIMutableApplicationShortcutItem *fourShortcutItem = [[UIMutableApplicationShortcutItem alloc] initWithType:@"fourType" localizedTitle:@"标题" localizedSubtitle:@"Subtitle" icon:fourIcon userInfo:nil];

UIApplicationShortcutIcon *fiveIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];

application.shortcutItems = @[threeShortcutItem, fourShortcutItem];

说明:也可以把UIApplicationShortcutIcon 改成可变的,在程序运行时改变action的title,subTitle都可实现。图片可以用系统提供的,也可自己设计。

  3.点击事件处理:

  3.1点击事件的处理(后台):当app在后台的时候UIApplication提供了一个回调方法

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler; 

依据这个回调中的shortcutItem的type和userinfo来做出不同的事件处理

  3.2点击事件的处理(开进程):如果是快捷键直接开进程的话,didFinishLaunchingWithOptions返回YES的情况下,didFinishLaunchingWithOptions方法和performActionForShortcutItem方法都会走。官方示例代码中给出的是直接开进程时didFinishLaunchingWithOptions方法返回NO,这样就不走performActionForShortcutItem方法了。这种情况下需要直接在didFinishLaunchingWithOptions方法中处理事件。

UIApplication又给我们一个从launchOptions中获取这个shortcutItem的key(UIApplicationLaunchOptionsShortcutItemKey)
UIApplicationShortcutItem *item = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];根据action响应事件即可。











转载于:https://my.oschina.net/u/2560887/blog/549948

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值