2012-12-19 8 views
4

사용자가 파일 목록을 관리 할 수있는 기본 코코아 응용 프로그램이 있습니다. 파일은 드래그 & drop을 통해 추가되며 응용 프로그램이 다시 시작될 때마다 보안 책갈피에 액세스 권한을 유지합니다.응용 프로그램 샌드 박스 : 파일 이름 바꾸기가 작동하지 않습니다.

지금까지 그렇게 좋았습니다. 앱에서 사용자 파일을 읽고 쓸 수 있지만 내 앱이 상위 폴더에 액세스 할 수 없다고 주장하는 권한 오류로 이름 변경이 실패합니다.

코드 :

[[NSFileManager defaultManager] moveItemAtPath:currentPath 
             toPath:newPath error:&error] 

오류 :

Error Domain=NSCocoaErrorDomain Code=513 "“Some image.jpg” couldn’t 
be moved because you don’t have permission to access “some folder” 

나는이 어제 일을하는 데 사용 맹세, 아무것도 ... 어쨌든 변경되지 않습니다. 사용자가 열기 대화 상자를 통해 파일에 액세스 할 수 있거나 &을 드래그하면 샌드 박스 응용 프로그램에서 파일의 이름을 바꿀 수 있다고 가정합니다. ,

규칙은 ...

파일을 (실제로 이동 작업을 수행)의 이름을 변경 :

+0

당신이 확실히 안전한 북마크를 해결하고 있습니까
다음은 발췌 한 것입니까? – Dov

+0

예, 확실히 북마크를 해결하고 보안 액세스를 시작합니다. – Mark

+0

더 많은 코드를 제공해 주시겠습니까? 누군가 문제를 재연하기에 충분할 것 같습니까? 지금은 계속할 일이별로 없습니다. 또한 출발 경로와 도착 경로에 액세스하고 있습니까? – Dov

답변

3
당신이 직면하고있는 문제는 아무것도 지속에 대한 아니다

, 그것은 샌드 박스가 작동하는 방법에 관하여
해당 파일의 상위 디렉토리에 대한 쓰기 권한이 있어야합니다.

우리의 문제가 ...

당신이 파일을 드래그하면, 샌드 박스 지금이 아닌 상위 디렉토리는, 따라서이 오류가 말하는 그 권한

을 필요로 이러한 파일에 대한 액세스를 확장한다 이 파일이 들어있는 폴더를 드래그하면 모든 것이 제대로 작동하는 것을 알 수 있습니다 :)

그럼 어떻게해야합니까?

간단한 해결책은 샌드 박스는 사용자에게 그가
의 이름을 변경하고자 할 때마다 소외없이 액세스 할 수 있도록 NSOpenPanel를 통해 "작업"디렉토리를 선택하도록 요청하는 것입니다하지만 우리는 지금 사용자를 귀찮게하고 쓰레기 그는 처음부터 알지 못해!
그래서 나에게 그건 나쁜 디자인/UX라는 방법을했다

지금 내가 샌드 박스에 대한 문서를 읽은와 나는 NSFileCoordinator을 발견 itemAtURL:willMoveToURL:

느릅 나무는 (내가 여기 다시 썼다 것을이 작은 snippet를 알려준 , 슬프게도

NSURL *sourceURL = document.fileURL; 
NSURL *destinationURL = [[sourceURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:fileName isDirectory:NO]; 

NSError *writeError = nil; 
__block NSError *moveError = nil; 
__block BOOL success = NO; 

NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; 

[coordinator coordinateWritingItemAtURL:sourceURL 
           options:NSFileCoordinatorWritingForMoving 
         writingItemAtURL:destinationURL 
           options:NSFileCoordinatorWritingForReplacing 
            error:&writeError 
          byAccessor:^(NSURL *newURL1, NSURL *newURL2) 
{ 
    NSFileManager *fileManager = [NSFileManager new]; 

    [coordinator itemAtURL:sourceURL willMoveToURL:destinationURL]; 

    success = [fileManager moveItemAtURL:newURL1 toURL:newURL2 error:&moveError]; 

    if (success) 
    { 
     [coordinator itemAtURL:newURL1 didMoveToURL:newURL2]; 
    } 
}]; 

을 : 그것은 willMove 기능)이 누락 되었기 때문에
은 우리가 여기서하고 싶은 이름 변경을 위해서 샌드 박스 확장을 요구하다 만큼으로 내가 추가 날 위해 야호,

NSFileSandboxingRequestRelatedItemExtension: an error was received from pboxd instead of a token. Domain: NSPOSIXErrorDomain, code: 1 

야호, 사과

+0

이동하는 대신 이름을 바꾸기위한 API가 필요한 것처럼 들릴 수 있습니다. 필자의 경우 부모 폴더는 그대로 유지되며 사용자는 실제로 파일 이름 만 변경합니다. – Mark

2

다음 작품이 메소드는 로그에, 따라서이 오류를 파일 확장자를 변경하기보다는 이름 바꾸기를 대상으로 보인다 관련 항목 문서 유형이 Info.plistmyext입니다. 이 내용은 Apple's Documentation에 나와 있습니다.

In both scenarios, you must make a small change to the application’s Info.plist file. Your app should already declare a Document Types (CFBundleDocumentTypes) array that declares the file types your app can open.

For each file type dictionary in that array, if that file type should be treated as a potentially related type for open and save purposes, add the key NSIsRelatedItemType with a boolean value of YES.

코드 BenRhayader의 대답에서 수정 : 당신이 경로에 액세스하기 전에

NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; 
    NSURL *sourceURL = chosenFile; 
    NSURL *destinationURL = [chosenFile URLByAppendingPathExtension: @"myext"]; 

    [coordinator coordinateWritingItemAtURL:sourceURL 
            options:NSFileCoordinatorWritingForMoving 
          writingItemAtURL:destinationURL 
            options:NSFileCoordinatorWritingForReplacing 
             error:NULL 
           byAccessor:^(NSURL *newURL1, NSURL *newURL2) 
    { 
     NSFileManager *fileManager = [NSFileManager new]; 

     [coordinator itemAtURL:sourceURL willMoveToURL:destinationURL]; 
     NSError *moveError; 
     BOOL success = [fileManager moveItemAtURL:newURL1 toURL:newURL2 error:&moveError]; 

     if (success) 
     { 
      [coordinator itemAtURL:newURL1 didMoveToURL:newURL2]; 
     } 
    }]; 
+0

불행히도이 방법은 모든 파일 형식에서 작동하지 않을 것입니다 ... – Mark

+0

맞습니다. Apple 설명서를 기반으로하면 "도매"유형의 권한을 요청할 수없는 것처럼 들리며, 다시 응용 프로그램이 사용됩니다. –

+0

폴더/패키지에는 작동하지 않습니다. (적어도 그것은 나를 위해 일하지 않았다). 어쨌든 고맙겠습니다 :-) – codingFriend1