Hi,

I am using this and I guess it is the recommended way of doing. Works well: http://developer.apple.com/mac/library/samplecode/BetterAuthorizationSample/index.html

Volker

Am 12.11.2009 um 18:44 schrieb JongAm Park:

Hello,

I'm trying to copy a file from a desktop to a /Library/Application Supports/../Plugins directory. Because the directory requires to obtain system administrator's privilege I should be authorized.

so, I wrote codes like this.

- (void)awakeFromNib
{
  [self authorize];
}

- (void) applicationWillTerminate:(NSNotification *)aNotification
{
  OSStatus myStatus;
  NSLog( @"applicationWillTerminate");
myStatus = AuthorizationFree( mAuthorizationRef, kAuthorizationFlagDestroyRights );
 }

-(IBAction)copyWithNSFileManager:(id)sender
{
NSString *PlugInPath = [NSString stringWithString:@"/Library/ Application Support/Final Cut Pro System Support/Plugins/test3.valm"]; NSString *sourceFile = [NSString stringWithFormat:@"%@/Desktop/ test3.valm", NSHomeDirectory()];
    BOOL isSuccessful;
isSuccessful = [[NSFileManager defaultManager] copyPath:sourceFile toPath:PlugInPath handler:nil];
}

-(IBAction)copyWithNSWorkspace:(id)sender
{
NSString *PluginPath = [NSString stringWithString:@"/Library/ Application Support/Final Cut Pro System Support/Plugins"]; NSString *sourceDirectory = [NSString stringWithFormat:@"%@/ Desktop", NSHomeDirectory()];
  NSArray *files = [NSArray arrayWithObject:@"test3.valm"];
    NSInteger tag;
BOOL isSuccessful = [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceCopyOperation
                                               source:sourceDirectory
                                          destination:PluginPath
                                                files:files
                                                  tag:&tag];
    NSLog(@"isSuccessful = %@", isSuccessful? @"YES":@"NO" );
}

- (void)authorize
{      OSStatus myStatus;
myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &mAuthorizationRef);
    AuthorizationItem myItems[1];
    myItems[0].name = "com.greenleaf.FileCopyTest.myRight1";
  myItems[0].valueLength = 0;
  myItems[0].value = NULL;
  myItems[0].flags = 0;
      AuthorizationRights myRights;
  myRights.count = sizeof( myItems ) / sizeof( myItems[0] );
  myRights.items = myItems;
AuthorizationFlags myFlags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights; myStatus = AuthorizationCopyRights(mAuthorizationRef, &myRights, kAuthorizationEmptyEnvironment, myFlags, NULL);
  if( myStatus == errAuthorizationSuccess )
  {
      NSLog(@"Successful in authorizing.. " );
  }
 }

Although it returns "errAuthorizationSuccess" in "authorize" method, when a file is to be copied, it returns "fail" state and the file is not copied.
Can anyone help me to figure out why?

Should I use AuthorizationExecuteWithPrivileges() and invoke an external command like "cp"? I think it should be possible to copy files with proper privilege with Cocoa calls.

Thank you.
JongAm Pawrk
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/volker_lists%40ecoobs.de

This email sent to volker_li...@ecoobs.de

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to