macOS 开发 - 添加 Document Type (注册文件打开方式)


简述

右键点击图片,打开方式中可以选择不同的app,如果希望我们的app,在右键选择时,能够出现关联,则需要添加 Document Type。

这里写图片描述


添加方法

方法一

1、在 Targets --> Info --> Document Types 里面添加需要的类型。
比如我需要关联 gif、png、webP
这里写图片描述


其中 identifier 是一个常量,不能随便设置,可参考官方文档:
https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html


2、在 delegate中添加代码

-(void)application:(NSApplication *)application openURLs:(NSArray<NSURL *> *)urls{
    
    NSLog(@"urls : %@",urls);
}

验证 & 运行效果

添加完成后,运行程序。运行时,系统会将该app的document type 注册上去。
找到文件夹中的png,或者gif 图片,右击–>打开方式,就可以看到我们的app了。
选中我们的app后,程序打印台会打印类似如下日志:

2018-03-07 15:07:22.910279+0800 MacCodeGen[58066:2351042] urls : (
    "file:///Users/MelissaShu/Pictures/pathForResource.png"
)


方法二

添加后,内容也会体现在 info.plist 中
这里写图片描述


使用sourceCode 查看,可以发现增加了如下代码:


    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>gif</string>
            </array>
            <key>CFBundleTypeName</key>
            <string>GIF image</string>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>LSHandlerRank</key>
            <string>Default</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>com.compuserve.gif</string>
            </array>
        </dict>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>png</string>
            </array>
            <key>CFBundleTypeName</key>
            <string>PNG image</string>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>LSHandlerRank</key>
            <string>Default</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>public.png</string>
            </array>
        </dict>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>webp</string>
            </array>
            <key>CFBundleTypeName</key>
            <string>WebP image</string>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>LSHandlerRank</key>
            <string>Default</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>com.google.webp</string>
                <string>public.webp</string>
            </array>
        </dict>
    </array>

也可以直接将上述代码添加到 info.plist 的code 中来设置支持格式。


CFBundleDocumentTypes 中的字段

CFBundleDocumentTypes 数组中的每个 dictionary 可能包含以下键:


CFBundleTypeName

指定文档类型名称。


CFBundleTypeExtensions

文件类型,比如:pdf,doc,xls等,不可乱填,


CFBundleTypeIconFiles

是一个数组,包含多个图片文件名,用于作为该文档的图标,显示在活动列项中。一般用的是app的icon。


Document Content Type UTIs

指定官方指定的文件类型,


参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI工程仔

请我喝杯伯爵奶茶~!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值