Monday, April 29, 2013

NSBundle bundleWithURL example ios


bundleWithURL:

Returns an NSBundle object that corresponds to the specified file URL.
+ (NSBundle *)bundleWithURL:(NSURL *)url
Parameters
url
The URL to a directory. This must be a URL for a directory; if it contains any symbolic links, they must be resolvable.
Return Value of [NSBundle bundleWithURL]
The NSBundle object that corresponds to url, or nil if url does not identify an accessible bundle directory.
Discussion of [NSBundle bundleWithURL]
This method allocates and initializes the returned object if there is no existing NSBundleassociated with url, in which case it returns the existing object.
Example of [NSBundle bundleWithURL] - 1
@interface CSResourceManager

+ (NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type;

@end

@implementation CSResourceManager

+ (NSArray *)resourceBundles
{
    NSBundle *mainBundle = [NSBundle mainBundle];

    NSURL *libraryBundleURL = [mainBundle URLForResource:@"MyLibraryBundle" withExtension:@"bundle"];
    NSBundle *libraryBundle = [NSBundle bundleWithURL:libraryBundleURL];

    // Add more bundles here and/or cache as necessary
    return @[ libraryBundle, mainBundle ];
}

+ (NSString *)pathForResource:(NSString *)resource ofType:(NSString *)type
{
    NSString *path = nil;
    for (NSBundle *bundle in [self resourceBundles]) {
        if ((path = [[self resourceBundle] pathForResource:resource ofType:type])) {
            break;
        }
    }
    return path;
}

@end
Example of [NSBundle bundleWithURL] - 2

NSBundle *libBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"BundleName" withExtension:@"bundle"]];
Then load the nib with this:
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:libBundle];