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. 實現(xiàn)一個(BOOL)handleShortcutItem:(UIApplicationShortcutItem *)shortcutItemBOOL值的方法, 里面進行業(yè)務操作
  2. 實現(xiàn)代理方法:
- (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 一些鍵值的說明
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評論 19 139
  • 專著:http://www.itdecent.cn/p/3443a3b27b2d 1.簡單的介紹一下3D Touc...
    violafa閱讀 1,104評論 1 0
  • 前言 關于這篇文章 由于iPhone 6S發(fā)布不到一年的時間,很多新特性、新技術(shù)還未普遍,不管是3D Touch的...
    Tangentw閱讀 4,744評論 8 18
  • 一、屏幕圖標使用3D Touch創(chuàng)建快速進入入口: 1、與之相關的類: (1)、 UIApplicationSho...
    尋形覓影閱讀 870評論 0 0
  • 3D Touch介紹 從iPhone 6s開始,產(chǎn)品都添加了一項硬件屬性,叫做3D touch。作為屏幕的一部分,...
    歪筆書生_閱讀 717評論 0 0

友情鏈接更多精彩內(nèi)容