iOS- Home Screen Quick Actions

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

   2.快捷键的建立 app

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


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

      UIApplicationShortcutItemTitle(required) 标题,在无子标题时若是标题太长可自动换行      spa

   UIApplicationShortcutItemSubtitle     子标题,在标题的下方 设计

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

   UIApplicationShortcutItemIconFile   自定义一个图标,以单一颜色单一35*35的大小展现,若是设置这个,UIApplicationShortcutItemIconType将不起做用 orm

   UIApplicationShortcutItemUserInfo   字典,里面能够添加各类key,value。 对象

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


@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响应事件便可。
相关文章
相关标签/搜索