Monday, April 29, 2013

NSBundle initWithPath example ios


Returns an NSBundle object initialized to correspond to the specified directory.
- (id)initWithPath:(NSString *)fullPath
The path to a directory. This must be a full pathname for a directory; if it contains any symbolic links, they must be resolvable.
Return Value of [NSBundle initWithPath]
An NSBundle object initialized to correspond to fullPath. This method initializes and returns a new instance only if there is no existing bundle associated with fullPath, otherwise it deallocatesself and returns the existing object. If fullPath doesn’t exist or the user doesn’t have access to it, returns nil.
Discussion of [NSBundle initWithPath]
It’s not necessary to allocate and initialize an instance for the main bundle; use the mainBundleclass method to get this instance. You can also use the bundleWithPath: class method to obtain a bundle identified by its directory path.
Example of [NSBundle initWithPath]-1
NSArray *bundleArr = [[NSBundle mainBundle]pathsForResourcesOfType:@"bundle" inDirectory:nil];
NSLog(@"pdfs in bundle %@ is my class is %@",[bundleArr objectAtIndex:0],[[bundleArr objectAtIndex:0]class]);

for (int i=0; i<[bundleArr count]; i++) {
    NSString *myBundleStr=[bundleArr objectAtIndex:i];
    NSBundle *myBundle = [[NSBundle alloc]initWithPath:[bundleArr objectAtIndex:i]];
    NSArray *pdfPaths = [myBundle pathsForResourcesOfType:@"pdf" inDirectory:nil];
    NSLog(@"\n\nPDF in bundle  is %@",pdfPaths);


Example of [NSBundle initWithPath] - 2
     //get the path to the bundle
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"localizable" ofType:@"strings" inDirectory:nil forLocalization:lang];
    NSLog(@"bundlePath = %@",bundlePath);

    //load the bundle
    NSBundle *langBundle = [[NSBundle alloc] initWithPath:[bundlePath stringByDeletingLastPathComponent]];
    NSLog(@"langBundle = %@",langBundle);
    return langBundle;