iOS应用3D Touch快捷访问

用法

添加快捷项(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 一些键值的说明
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,410评论 19 139
  • 专著:http://www.jianshu.com/p/3443a3b27b2d 1.简单的介绍一下3D Touc...
    violafa阅读 1,084评论 1 0
  • 前言 关于这篇文章 由于iPhone 6S发布不到一年的时间,很多新特性、新技术还未普遍,不管是3D Touch的...
    Tangentw阅读 4,729评论 8 18
  • 一、屏幕图标使用3D Touch创建快速进入入口: 1、与之相关的类: (1)、 UIApplicationSho...
    寻形觅影阅读 857评论 0 0
  • 3D Touch介绍 从iPhone 6s开始,产品都添加了一项硬件属性,叫做3D touch。作为屏幕的一部分,...
    歪笔书生_阅读 705评论 0 0

友情链接更多精彩内容