Monday, April 29, 2013

NSBundle preflightAndReturnError example ios


preflightAndReturnError:

Returns a Boolean value indicating whether the bundle’s executable code could be loaded successfully.
- (BOOL)preflightAndReturnError:(NSError **)error
Parameters
error
On input, a pointer to an error object variable. On output, this variable may contain an error object indicating why the bundle’s executable could not be loaded. If no error would occur, this parameter is left unmodified. You may specify nil for this parameter if you are not interested in the error information.
Return Value of [NSBundle preflightAndReturnError]
YES if the bundle’s executable code could be loaded successfully or is already loaded; otherwise,NO if the code could not be loaded.
Discussion of [NSBundle preflightAndReturnError]
This method does not actually load the bundle’s executable code. Instead, it performs several checks to see if the code could be loaded and with one exception returns the same errors that would occur during an actual load operation. The one exception is the NSExecutableLinkErrorerror, which requires the actual loading of the code to verify link errors.
For a list of possible load errors, see the discussion for the loadAndReturnError: method.
Example of [NSBundle preflightAndReturnError]

- (void)loadSystemFrameworks  // Contributed by Cedric Luthi
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSMutableArray *systemFrameworksPaths = [NSMutableArray arrayWithObject:@"/System/Library/Frameworks"];

  if ([[NSUserDefaults standardUserDefaults] boolForKey:@"FScriptLoadPrivateSystemFrameworks"])
    [systemFrameworksPaths addObject:@"/System/Library/PrivateFrameworks"];

  for (NSString *systemFrameworksPath in systemFrameworksPaths)
  {
    for (NSString *framework in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:systemFrameworksPath error:NULL])
    {
      NSBundle *frameworkBundle = [NSBundle bundleWithPath:[systemFrameworksPath stringByAppendingPathComponent:framework]];
      if ([frameworkBundle preflightAndReturnError:nil])
        [frameworkBundle load];
    }
  }

  [pool drain];
}