Objective c 以编程方式将应用程序的内容复制到桌面
如何以编程方式将整个Objective c 以编程方式将应用程序的内容复制到桌面,objective-c,macos,cocoa,Objective C,Macos,Cocoa,如何以编程方式将整个.app捆绑包([[NSBundle mainBundle]bundleURL])复制到桌面 这是我的代码,但对我没有帮助 NSString *sourcepath = [[NSBundle mainBundle] bundlePath]; NSString *destpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop"]; [[NSFileManager defaultManager] co
.app
捆绑包([[NSBundle mainBundle]bundleURL]
)复制到桌面
这是我的代码,但对我没有帮助
NSString *sourcepath = [[NSBundle mainBundle] bundlePath];
NSString *destpath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop"];
[[NSFileManager defaultManager] copyItemAtPath:sourcepath toPath:destpath error:nil];
您可以使用标准的
NSFileManager
方法
- 或
请注意,如果某个项目(文件/目录)已经存在,则必须手动删除该项目(文件/目录),否则复制将失败(同样,根据Apple的文档)。您可以使用标准的
NSFileManager
方法
- 或
请注意,如果某个项目(文件/目录)已经存在,则必须手动删除该项目(文件/目录),否则复制将失败(同样,根据苹果的文档)。以下是我解决该问题的方法。这可能对某些人有所帮助
- (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];
}
这是我解决问题的方法,也许这对某人有帮助
- (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];
}
在用户的硬盘上创建应用程序的克隆真的有意义吗?你到底想达到什么目的?桌面上的alias之类的窗口?实际上我只是想知道如何在objective c中以编程方式复制。我尝试使用[[NSFileManager defaultManager],但它对我没有帮助,可能是我的错误。您是否添加了错误处理和
NSLog()
-ged是否返回n错误
?在用户硬盘上创建应用程序的克隆真的有意义吗?你到底想实现什么?桌面上的alias之类的窗口?实际上我只是想知道如何在objective c中以编程方式复制。我尝试使用[[NSFileManager defaultManager]但是这对我没有帮助,我可能在某个地方出错了。您是否添加了错误处理和NSLog()
-ged是否返回n错误
?能否请您重新查看我的问题?我已通过插入代码对其进行了编辑。能否请您重新查看我的问题?我已通过插入代码对其进行了编辑。始终检查和处理文件操作的潜在错误,即检查copyItemAtPath:call returnsYES
并为方法调用提供一个NSError
引用以获取错误详细信息。始终检查并处理文件操作的潜在错误肯定是有价值的,例如,我将检查copyItemAtPath:call是否返回YES
,并为错误检测的方法调用提供一个NSError
引用ils。