用法
添加快捷项(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]
良好实践
- 实现一个
(BOOL)handleShortcutItem:(UIApplicationShortcutItem *)shortcutItem
返BOOL
值的方法, 里面进行业务操作 - 实现代理方法:
- (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;
试试吧
网友评论