2011-06-20 18 views
0

プロジェクトバンドルにあるアプリケーションをユーザーのアプリケーションフォルダに移動しようとしています。バンドルされた "testApp.app"は、プロジェクトのResourcesフォルダにあります。私のコードは完了していません。ユーザーのシステム上のグローバルアプリケーションフォルダをどのように指しているか、何か問題がありますか?バンドルされたアプリケーションをアプリケーションフォルダに送信

ありがとうございました。

ポール

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]; 

答えて

2

このような何か試してください:あなたのコードでは、あなたが/Users/<username>/Applicationsのようなものに拡大するものにappDirectoryセットを持っていたし、次の行はこのようなものと同一視しているだろうと

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); 
} 

注:

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

これはおそらくあなたが望むものではありません。

+0

ありがとうございます。 – Paul

3

.appを「ファイル」は、実際のファイルではありません、彼らはディレクトリです(右のいずれかと「表示内容」をクリックしてください)。したがって、そのようにコピーする必要があります。あなただけのメッセージを渡すのではなく、戻り値を割り当てる必要がありますので、同様にあなたのコードを使用して他のいくつかの問題があります

は、[NSString stringBy...]メッセージがNSString以来、新しい文字列を返すには、不変です。

1

アンディ・キムは彼のソリューション(LetsMove)をGitHubに公開しました。あなたはそれを見つけることができますhere

関連する問題