将捆绑的应用程序发送到“应用程序"文件夹 [英] Sending bundled application to Applications folder

查看:75
本文介绍了将捆绑的应用程序发送到“应用程序"文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将项目捆绑包中的应用程序移至用户的Applications文件夹.捆绑的"testApp.app"位于项目的Resources文件夹中.我的代码没有完成.我如何指向用户系统上的全局Applications文件夹有问题吗?

I'm trying to move an application that is located in my project bundle to the users Applications folder. The bundled "testApp.app" is located in the projects Resources folder. My code is not getting it done. Is there something wrong with how I am pointing to the global Applications folder on the user's system?

谢谢.

保罗

NSFileManager* fileManager = [NSFileManager defaultManager];

NSError *error = nil;

NSString *appDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *path =  [appDirectory stringByAppendingPathComponent:@"~/Applications/testApp.app"];
[path stringByExpandingTildeInPath];


if ([fileManager fileExistsAtPath:path isDirectory:NULL]) {
[fileManager removeItemAtPath:path error:&error];

}

if(![fileManager fileExistsAtPath:path]){       
}


NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/testApp.app"]];
[data writeToFile:path atomically:YES];

推荐答案

尝试如下操作:

NSString *sourcePath = [[NSBundle mainBundle] 
                    pathForResource:@"testApp" ofType:@"app"];
if (sourcePath == nil) {
    NSLog(@"testApp.app was not found");
    return;
}
NSArray *appsFolders = NSSearchPathForDirectoriesInDomains(
             NSApplicationDirectory, NSUserDomainMask, YES);
if ([appsFolders count] == 0) {
    NSLog(@"~/Applications/ not found");
    return;
}
NSString *appsFolder = [appsFolders objectAtIndex:0];
NSString *destPath = [appsFolder stringByAppendingPathComponent:@"testApp.app"];
NSFileManager *fileManager = [NSFileManager defaultManager];

[fileManager removeItemAtPath:destPath error:&error];

if (![fileManager copyItemAtPath:sourcePath toPath:destPath error:&error]) {
    NSLog(@"failed to copy %@ to %@", sourcePath, destPath);
}

请注意,在您的代码中,您已将 appDirectory 设置为可以扩展为/Users/< username>/Applications 之类的内容,并且下一行将等于这样的东西:

Note that in your code, you had appDirectory set to something that would expand to something like /Users/<username>/Applications, and the following line would have equated to something like this:

NSString *path = [@"/Users/<username>/Applications"
 stringByAppendingPathComponent:@"~/Applications/testApp.app"];

可能不是您想要的.

这篇关于将捆绑的应用程序发送到“应用程序"文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆