Resource-only NSBundle: is this kosher?
In my iOS app, I'm downloading content from the web into my /Library/Caches directory. I'd like to represent this directory as an NSBundle for better compatibility with some of the external APIs we're using. (That way, we can simply change [[NSBundle mainBundle] pathForResource...] to [myBundle pathForResource...] whenever it appears.)
The following seems to work fine:
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* cachesDirectory = [paths objectAtIndex:0];
NSBundle* bundle = [NSBundle bundleWithPath:cachesDirectory];
Better yet, the bundle reflects any changes I make to the /Library/Caches directory. However, I'm concerned because the caches directory is technically not a bundle per Apple's docs. That is to say:
- It's not a "directory with a standardized hierarchical structure that holds execut开发者_运维百科able code and the resources used by that code", since there's no code.
- It's neither an Application, Framework, nor a Plug-In bundle.
- It's most like an Application bundle, but it doesn't contain the required Info.plist or executable.
I could find no mention anywhere of this sort of dynamically-created, resource-only bundle. Is this okay to do?
The /Library/Caches
directory will lack some of the standard files which are required in a bundle, like a Contents/
directory or a Contents/Info.plist
file, so it may not behave properly when treated as one. Proceed with caution.
Yes, it's absolutely okay to have a resources only bundle. Some of the verbage that you quote pre-exists iOS. In OS X you can dynamically load executable code, that's specifically excluded in iOS.
Localization is an example of resource only bundles.
Edit:
The Bundle Programming Guide says:
Although document formats can leverage the bundle structure to organize their contents, documents are generally not considered bundles in the purest sense. A document that is implemented as a directory and treated as an opaque type is considered to be a document package, regardless of its internal format. For more information about document packages, see “Document Packages.”
which says:
There are several ways to access the contents of a document package. Because a document package is a directory, you can access the document's contents using any appropriate file-system routines. If you use a bundle structure for your document package, you can also use the NSBundle or CFBundleRef routines. Use of a bundle structure is especially appropriate for documents that store multiple localizations.
also note that Apple has been telegraphing that it is minimizing the use of "path"/NSString APIs in favor of URL APIs, though existing path APIs will no doubt continue for many more major OS releases.
精彩评论