iOS应用3D Touch快捷访问

2017-07-04  本文已影响37人  walkerwzy

用法

添加快捷项(UIApplicationShortcutItem)

有两种途径, 编辑Info.plist或代码添加

Info.plist

<key>UIApplicationShortcutItems</key>
<array>
    <dict>
       <!--图标, 必须-->
        <key>UIApplicationShortcutItemIconType</key>
        <string>UIApplicationShortcutIconTypeCapturePhoto</string>
        <!--标题, 必须-->
        <key>UIApplicationShortcutItemTitle</key>
        <string>Scan</string>
        <!-副标题-->
        <key>UIApplicationShortcutItemSubtitle</key>
        <string>QR Code</string>
        <!--快捷项标识符-->
        <key>UIApplicationShortcutItemType</key>
        <string>$(PRODUCT_BUNDLE_IDENTIFIER).Scan</string>
    </dict>
</array>

完整可选项见文档

代码添加

// Construct the items.
let shortcut3 = UIMutableApplicationShortcutItem(
    type: ShortcutIdentifier.Third.type, 
    localizedTitle: "Play", 
    localizedSubtitle: "Will Play an item", 
    icon: UIApplicationShortcutIcon(type: .play), 
    userInfo: [
        AppDelegate.applicationShortcutUserInfoIconKey: UIApplicationShortcutIconType.play.rawValue
    ]
)
    
let shortcut4 = ... // 同上
    
// Update the application providing the initial 'dynamic' shortcut items.
application.shortcutItems = [shortcut3, shortcut4]

良好实践

  1. 实现一个(BOOL)handleShortcutItem:(UIApplicationShortcutItem *)shortcutItemBOOL值的方法, 里面进行业务操作
  2. 实现代理方法:
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    completionHandler([self handleShortcutItem:shortcutItem]);
}
  1. didBecomeActive方法里判断是否需要 handle 快捷方式
- (void)applicationDidBecomeActive:(UIApplication *)application {
    if(!self.launchedShortcutItem) return;
    [self handleShortcutItem:self.launchedShortcutItem];
    self.launchedShortcutItem = nil;
}
  1. 3说明如果你需要提取一个属性launchedShortcutItem
  2. 如果提取了属性, 那么didFinishLaunch也可以顺便改为:
BOOL shouldPerformAdditionalDelegateHandling = YES;
UIApplicationShortcutItem *shortcutItem = (UIApplicationShortcutItem *)launchOptions[UIApplicationLaunchOptionsShortcutItemKey];
if(shortcutItem) {
    self.launchedShortcutItem = shortcutItem;
    shouldPerformAdditionalDelegateHandling = NO;
}

// 你的其它初始代码
   
return shouldPerformAdditionalDelegateHandling;  // 通常这里返的是 YES;

试试吧

参考资料

  1. 官方文档
  2. 示例代码
  3. 快捷图标
  4. 模拟器支持
  5. iOS Keys 一些键值的说明
上一篇下一篇

猜你喜欢

热点阅读