fazal
fazal

Reputation: 49

Copy contents of app to desktop programmatically

How can I copy my entire .app bundle ([[NSBundle mainBundle] bundleURL]) to my desktop programmatically?

Here is my code but not helping me.

NSString *sourcepath = [[NSBundle mainBundle] bundlePath]; 
NSString *destpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop"];
[[NSFileManager defaultManager] copyItemAtPath:sourcepath toPath:destpath error:nil];

Upvotes: 1

Views: 542

Answers (2)

fazal
fazal

Reputation: 49

Here's how I solved it may be this can be helpful for someone.

- (IBAction)createShortcut:(id)sender {

 NSString *sourcepath = [[NSBundle mainBundle] bundlePath];
 NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
 NSString *theDesktopPath = [paths objectAtIndex:0];
 NSString *saveFilePath = [theDesktopPath stringByAppendingPathComponent:@"myApp.app"];
 [[NSFileManager defaultManager] copyItemAtPath:sourcepath toPath:saveFilePath error:nil];

}

Upvotes: 0

Jay
Jay

Reputation: 6638

You can use the standard NSFileManager methods

to copy directories as well as files. From Apple's docs:

When copying items, the current process must have permission to read the file or directory at srcPath and write the parent directory of dstPath. If the item at srcPath is a directory, this method copies the directory and all of its contents, including any hidden files.

Mind that you have to manually remove an item (file/directory) at the destination with the same name if it already exists or else the copy will fail (again, as per Apple's docs).

Upvotes: 1

Related Questions