iOS开发实用技术Swift开发IOS开发

APP图标添加长按快捷菜单

2021-10-11  本文已影响0人  leblanc_i

官方文档链接
https://developer.apple.com/documentation/uikit/uiapplicationshortcutitem/
官方demo下载链接
https://docs-assets.developer.apple.com/published/127f683dd6/AddHomeScreenQuickActions.zip

添加方式有两种,静态添加动态添加

一、静态添加

直接在info.plist文件中添加(不想一个个添加,可以找到info.plist文件,以源码方式打开,然后直接复制)

<key>UIApplicationShortcutItems</key>
<array>
    <dict>
        <key>UIApplicationShortcutItemType</key>
        <string>SearchAction</string>
        <key>UIApplicationShortcutItemIconType</key>
        <string>UIApplicationShortcutIconTypeSearch</string>
        <key>UIApplicationShortcutItemTitle</key>
        <string>Search</string>
        <key>UIApplicationShortcutItemSubtitle</key>
        <string>Search for an item</string>
    </dict>
    <dict>
        <key>UIApplicationShortcutItemType</key>
        <string>ShareAction</string>
        <key>UIApplicationShortcutItemIconType</key>
        <string>UIApplicationShortcutIconTypeShare</string>
        <key>UIApplicationShortcutItemTitle</key>
        <string>Share</string>
        <key>UIApplicationShortcutItemSubtitle</key>
        <string>Share an item</string>
    </dict>
</array>
Key Description
UIApplicationShortcutItemType (required) 可以理解为菜单中 item 的标识,添加多个 item 的时候根据这个标识来区分点击事件
UIApplicationShortcutItemTitle (required) 菜单中 item 标题,最多显示两行,显示不全系统会自动在后面加上 ...
UIApplicationShortcutItemSubtitle 菜单中 item 副标题,最多显示两行,显示不全系统会自动在后面加上 ...
UIApplicationShortcutItemIconType 系统图标,枚举类型,关于所有图标可以自己尝试,或在文档中查看
UIApplicationShortcutItemIconFile 自定义图片名称(官方文档推荐图标应是 35 * 35 的单色,方形图片)
UIApplicationShortcutItemUserInfo 一个可选的,应用程序定义的字典

关于这些字段更详细的解释,可查看官方文档:https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW36
系统图标官方文档:
https://developer.apple.com/documentation/uikit/uiapplicationshortcuticon/icontype

二、动态添加

    func addShortcutItems() {
        
        // 自定义图标
        let shareIcon = UIApplicationShortcutIcon(templateImageName: "share")
        let shareItem = UIApplicationShortcutItem(type: "Share", localizedTitle: "分享", localizedSubtitle: nil, icon: shareIcon, userInfo: nil)
        
        // 系统图标
        let homeIcon = UIApplicationShortcutIcon(type: .home)
        let homeItem = UIApplicationShortcutItem(type: "home", localizedTitle: "首页", localizedSubtitle: nil, icon: homeIcon, userInfo: nil)
        
        // .fill后缀的图标名是 Apple 为我们提供了免费 SF 符号,可以在应用中使用,
        // 具体可查看官方文档:https://developer.apple.com/sf-symbols/
        let addIcon = UIApplicationShortcutIcon(systemImageName: "square.and.arrow.up.fill")
        let addItem = UIApplicationShortcutItem(type: "Add", localizedTitle: "添加", localizedSubtitle: nil, icon: addIcon, userInfo: nil)
        
        UIApplication.shared.shortcutItems = [shareItem, homeItem, addItem]
    }

    /// App没有启动,点击菜单item会打开App,然后走这个代理方法
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let _ = (scene as? UIWindowScene) else { return }
        
        if let shortcutItem = connectionOptions.shortcutItem {
            // 加0.1s延迟,然后继续之后的操作
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                self.showAlertAction(shortcutItem.type)
            }
        }
    }
 
    /// App已经启动,会走这个代理方法
    func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        showAlertAction(shortcutItem.type)
    }
    
    
    /// 菜单item点击事件
    func showAlertAction(_ type: String) {
        
        var msg: String = ""
        if type == "Add" {
            msg = "Add"
        } else if type == "Share" {
            msg = "Share"
        } else if type == "Home" {
            msg = "Home"
        }
        
        let alertVC = UIAlertController(title: "", message: msg, preferredStyle: .alert)
        let cancleAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        alertVC.addAction(cancleAction)
        self.window?.rootViewController?.present(alertVC, animated: true, completion: nil)
    }
    

注意点

App在屏幕下方.jpg
上一篇下一篇

猜你喜欢

热点阅读