用法
添加快捷項(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]
良好實踐
- 實現(xiàn)一個
(BOOL)handleShortcutItem:(UIApplicationShortcutItem *)shortcutItem返BOOL值的方法, 里面進行業(yè)務操作 - 實現(xiàn)代理方法:
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
completionHandler([self handleShortcutItem:shortcutItem]);
}
- 在
didBecomeActive方法里判斷是否需要 handle 快捷方式
- (void)applicationDidBecomeActive:(UIApplication *)application {
if(!self.launchedShortcutItem) return;
[self handleShortcutItem:self.launchedShortcutItem];
self.launchedShortcutItem = nil;
}
- 3說明如果你需要提取一個屬性
launchedShortcutItem - 如果提取了屬性, 那么
didFinishLaunch也可以順便改為:
BOOL shouldPerformAdditionalDelegateHandling = YES;
UIApplicationShortcutItem *shortcutItem = (UIApplicationShortcutItem *)launchOptions[UIApplicationLaunchOptionsShortcutItemKey];
if(shortcutItem) {
self.launchedShortcutItem = shortcutItem;
shouldPerformAdditionalDelegateHandling = NO;
}
// 你的其它初始代碼
return shouldPerformAdditionalDelegateHandling; // 通常這里返的是 YES;
試試吧