如何使Xcode创建的macOS文档类App识别.txt与.text文件
解决macOS文档类应用识别.txt/.text文件的步骤
直接编辑Info.plist文件(避开Xcode界面的bug)
- 找到项目里的
Info.plist文件,右键选「Open As」→「Source Code」 - 替换或添加
CFBundleDocumentTypes数组的内容,配置txt和text文件的关联:<key>CFBundleDocumentTypes</key> <array> <dict> <key>CFBundleTypeName</key> <string>Plain Text Document</string> <key>CFBundleTypeRole</key> <string>Editor</string> <key>LSHandlerRank</key> <string>Owner</string> <key>LSItemContentTypes</key> <array> <string>public.plain-text</string> <string>public.text</string> </array> </dict> </array> - 保存文件,回到Xcode,文档类型配置就生效了
- 找到项目里的
同步文档类的代码配置
- 找到项目默认的文档类(比如
OpenTextDocDocument),确保readableContentTypes属性返回正确的UTI类型:
Swift版本:
Objective-C版本:override class var readableContentTypes: [UTType] { return [.plainText, .text] }+ (NSArray<UTType *> *)readableContentTypes { return @[UTTypePlainText, UTTypeText]; }
- 找到项目默认的文档类(比如
清理重建项目
- 点击「Product」→「Clean Build Folder」(快捷键⇧⌘K)
- 重启Xcode后重新运行项目,此时打开文件对话框里的.txt和.text文件就能正常选中了
内容的提问来源于stack exchange,提问作者jsbox
相关产品推荐
相关产品推荐

