Question

I deal a lot with Lotus Notes in my company. I wrote a great application in C# to copy specific files to and from the user's Lotus Notes directory. Now I am wanting to write that application for OSX in Objective C. I have a handful of different files that need to be copied from ~/Library/Application Support/Lotus Notes Data/.

I am running into admin issues when I run a test to copy a single file. What is the best/easiest way(I am a beginner) to prompt the user for admin credentials and execute the file copy code with the newly acquired rights?

I did try implementing the BLAuthentication Class I found online, but it would not compile. I currently don't have access to my work computer to post the code.

Was it helpful?

Solution

Try like this below:-

NSString *path=[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)lastObject];
NSString *testUrl =[path stringByAppendingPathComponent:@"/LotusNotesData/source.rtf"];
//
if ([[NSFileManager defaultManager]fileExistsAtPath:testUrl]) {
    NSLog(@"yes");
}
//Below destination is folder name which should be exist on your machine or else you can create programmatically as well
NSString *testUrl2 = @"/Users/home/Documents/destination";

NSLog(@"%@",testUrl);
NSLog(@"%@",testUrl2);
NSError *err=nil;

//Now we are copying the souce path to destination folder with appending file name (it can be any your name becuase file manager copy source file contents to your destination file contents)
//Here given file name is a source.rtf where you can give any your name. Also this is for copying source contents to destination contents

NSFileManager *fm=[NSFileManager defaultManager];
if ([fm copyItemAtPath:testUrl toPath:[testUrl2 stringByAppendingPathComponent:@"source.rtf"] error:&err])
{
    NSLog(@"success");
}
else
{
    NSLog(@"%@",[err localizedDescription]);
}

OTHER TIPS

Use Apple Script to copy files with privilege access.

do shell script "cp source_path destination_path" with administrator privileges

where source path is path of file to be copied.

You may call the Apple script by adding a ".scpt" file with above script in your bundle and using code below:

- (void) runEmbeddedScriptFile: (NSString*)fileName
{
    NSString* path = [[NSBundle bundleForClass:[self class]] pathForResource:fileName ofType:@"scpt"];
    NSURL* url = [NSURL fileURLWithPath:path];
    NSDictionary* errors = [NSDictionary dictionary];
    NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
    [appleScript executeAndReturnError:nil];
    [appleScript release];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top