可以通过两种方法添加,info.plist添加方式,和通过代码[UIApplication sharedApplication].shortcutItems
添加。两种方式一起添加会合并菜单,默认只能显示4+(1)个菜单项。
1. info.plist 菜单添加方法
不灵活,如果多语言,这里无法实现支持
<key>UIApplicationShortcutItems</key>
<array>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeShare</string>
<key>UIApplicationShortcutItemTitle</key>
<string>分享</string>
<key>UIApplicationShortcutItemType</key>
<string>share</string>
</dict>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeAdd</string>
<key>UIApplicationShortcutItemTitle</key>
<string>添加</string>
<key>UIApplicationShortcutItemType</key>
<string>add</string>
</dict>
</array>
2. 通过代码创建
NSMutableArray* arr_items = [NSMutableArray arrayWithCapacity:3];
UIApplicationShortcutIcon *item_ico;
UIMutableApplicationShortcutItem *item;
item_ico = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCaptureVideo];
item = [[UIMutableApplicationShortcutItem alloc]initWithType:Shortcut_Album localizedTitle:LANGLOC(@"title_album") localizedSubtitle:nil icon:item_ico userInfo:nil];
[arr_items addObject:item];
item_ico = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
item = [[UIMutableApplicationShortcutItem alloc]initWithType:Shortcut_Share localizedTitle:LANGLOC(@"title_share") localizedSubtitle:nil icon:item_ico userInfo:nil];
[arr_items addObject:item];
[UIApplication sharedApplication].shortcutItems = [[arr_items reverseObjectEnumerator]allObjects];
图标展示
typedef NS_ENUM(NSInteger, UIApplicationShortcutIconType) {
UIApplicationShortcutIconTypeCompose,
UIApplicationShortcutIconTypePlay,
UIApplicationShortcutIconTypePause,
UIApplicationShortcutIconTypeAdd,
UIApplicationShortcutIconTypeLocation,
UIApplicationShortcutIconTypeSearch,
UIApplicationShortcutIconTypeShare,
UIApplicationShortcutIconTypeProhibit NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeContact NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeHome NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeMarkLocation NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeFavorite NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeLove NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeCloud NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeInvitation NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeConfirmation NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeMail NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeMessage NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeDate NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeTime NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeCapturePhoto NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeCaptureVideo NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeTask NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeTaskCompleted NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeAlarm NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeBookmark NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeShuffle NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeAudio NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeUpdate NS_ENUM_AVAILABLE_IOS(9_1)
} NS_ENUM_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED;
3. 自定义icon
item_ico = [UIApplicationShortcutIcon iconWithTemplateImageName:@"custom"];
item = [[UIMutableApplicationShortcutItem alloc]initWithType:@"Add" localizedTitle:LANGLOC(@"title_album") localizedSubtitle:nil icon:item_ico userInfo:nil];
[arr_items addObject:item];
4. 接收判断
- (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler {
// 根据 shortcutItem.type 判断点击的是哪个 item
if ([shortcutItem.type isEqualToString:@"Add"]) {
//代码
}
}
5. 系统自带的 ‘分享“app名称”’功能
不需要自己加入,发布到市场后就带这个功能,本地测试没有~
参考:
https://www.jianshu.com/p/9c6c17526da5
https://www.jianshu.com/p/9db27c2bc2d3
https://www.jianshu.com/p/d6c913c7973a