How to check whether directories with a given extension are shown by the Finder as a package?

泄露秘密 提交于 2019-12-06 23:48:35

You can query the Uniform Type Identifier information for your extension.
You first have to find out the correct UTI and then test if it conforms to com.apple.package (which is declared as kUTTypePackage):

- (BOOL)isPackageTypeForExtension:(NSString*)extension
{
    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)(extension), NULL);
    return UTTypeConformsTo(UTI, kUTTypePackage);
}

Update:
Additionally you could check against kUTTypeBundle:

 UTTypeConformsTo(UTI, kUTTypeBundle)

and OR (|) the result into the return value of above method.

The following Apple docs describe how to define the UTI for a document based app (which often use NSFileWrappers & document bundles): https://developer.apple.com/library/mac/documentation/DataManagement/Conceptual/DocBasedAppProgrammingGuideForOSX/ApplicationCreationProcess/ApplicationCreationProcess.html#//apple_ref/doc/uid/TP40011179-CH6-997699

Update2:
Additionally to the UTI conforming to kUTTypePackage or kUTTypeBundle, there is a Package Bit file attribute, which can also mark a folder as package:

From the Apple's documentation:

How the System Identifies Bundles and Packages The Finder considers a directory to be a package if any of the following conditions are true:

  • The directory has a known filename extension: .app, .bundle, .framework, .plugin, .kext, and so on.
  • The directory has an extension that some other application claims represents a package type; see “Document Packages.”
  • The directory has its package bit set.

Update3:
The UTI-based approach described above, does not cover all the cases the Finder takes into account when testing if a folder is a package or bundle.
It can be used to test if a specific UTI conforms to kUTTypeBundle or kUTTypePackage though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!