Mac 如何拖拽文件到dock上面的icon?(详细)

本文介绍在macOS应用中实现通过拖拽文件到Dock图标上直接打开的方法。需在AppDelegate.m中添加委托函数,并在Info.plist中配置文件类型信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

拖拽文件到dock上面的icon来打开需要两个操作:

1、代码

在AppDelegate.m里面添加delegate函数:

- (void)application:(NSApplication *)theApplication openFiles:(NSArray *)filenames

这个函数会自动被调用。

2、Info.plist

向Info.plist里面添加代码:

<key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>*</string>
            </array>
            <key>CFBundleTypeName</key>
            <string>Folder</string>
            <key>CFBundleTypeOSTypes</key>
            <array>
                <string>****</string>
            </array>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>LSTypeIsPackage</key>
            <false/>
        </dict>
    </array>

 

一开始我只添加了代码部分,结果不行,最后又添加了Info.plist部分,感觉苹果应该让步骤更简单点,添加代码部分就可以了,为何还要添加Info.plist部分。