2016-08-20 28 views
3

응용 프로그램이이 디렉토리에 파일을 쓰는 디렉토리에 대한 전체 읽기/쓰기 권한을 부여해야합니다. 나는 샌드 박스 응용 프로그램을 사용하여 응용 프로그램을 다시 실행 한 후 Enable Security-Scoped Bookmark and URL Access 폴더에 액세스해야한다고 읽었습니다. 디렉토리의 보안 범위가 지정된 책갈피

그래서 나는 약간의 수정 What is the correct way to handle stale NSURL bookmarks?

 NSOpenPanel* openDlg = [NSOpenPanel openPanel]; 
    [openDlg setCanChooseDirectories:YES]; 
    [openDlg setCanCreateDirectories:YES]; 
    [openDlg setAllowsMultipleSelection:FALSE]; 
    if ([openDlg runModal] == NSOKButton) 
    { 
     NSArray *files = [openDlg URLs]; 

     NSString* dirPath =[[files objectAtIndex:0] path];// absoluteString]; 
     BOOL isDirectory; 
     NSFileManager* manager = [NSFileManager defaultManager]; 



     NSString *Dir = [dirPath stringByAppendingPathComponent:@"ScreenCaptures"]; 
     if (![manager fileExistsAtPath:Dir isDirectory:&isDirectory] || !isDirectory) 
     { 
      NSError *error = nil; 

      [manager createDirectoryAtPath:Dir 
       withIntermediateDirectories:NO 
           attributes:nil 
            error:&error]; 
      if (error) 
       NSLog(@"Error creating directory snap path: %@", [error localizedDescription]); 

     } 


      NSURL *url = [NSURL URLWithString:[Dir stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

      NSData *bookmark = nil; 
      NSError *error = nil; 
      bookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope 
        includingResourceValuesForKeys:nil 
             relativeToURL:nil // Make it app-scoped 
               error:&error]; 
      if (error) { 
       NSLog(@"Error creating bookmark for URL (%@): %@", url, error); 
       [NSApp presentError:error]; 
      } 

      NSLog(@"bookmark: %@", bookmark); 
     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
     [userDefaults setObject:bookmark forKey:@"bookmark"]; 

    } 

그러나 위의 코드는 나에게 문제가 될 수 무엇 오류

016-08-20 02:19:53.390 FileAccess[635:85753] modalSession has been exited prematurely - check for a reentrant call to endModalSession: 
2016-08-20 02:19:59.979 FileAccess[635:85753] Error creating bookmark for URL (/Users/development/Documents/c/ScreenCaptures): Error Domain=NSCocoaErrorDomain Code=262 "Scoped bookmarks can only be made with file URLs" UserInfo={NSURL=/Users/development/Documents/c/ScreenCaptures, NSDebugDescription=Scoped bookmarks can only be made with file URLs} 
2016-08-20 02:20:00.021 FileAccess[635:85753] CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme 
2016-08-20 02:20:04.967 FileAccess[635:85753] bookmark: (null) 

을주는 여기에 코드를 기반으로 그것을 구현하기 위해 노력하고 있어요? 위의 코드에 잘못된 것이 있습니다.

답변

4

두 번째 오류 메시지는 무엇이 잘못되었는지 알려주고 file : // URL을 사용하지 않았습니다.

경로 변수에서 URL을 올바르게 작성하면이 문제를 해결할 수 있지만 URL을 그대로 사용하고 URL -> 경로 -> URL 변환을 수행하지 않는 것이 좋습니다. 경로를 사용한 모든 작업은 URL로 직접 수행 할 수 있습니다. NSFileManagerNSURL에 대한 설명서를 확인하십시오. 명확하지 않을 수있는 유일한 방법은 NSFileManagerfileExistsAtPath: 대신 NSURLcheckResourceIsReachableAndReturnError:을 사용하는 것입니다. 그러나 checkResourceIsReachableAndReturnError:의 설명서를주의 깊게 읽고 조언을 따르십시오.

이러한 변경 사항은보고 한 오류 중 적어도 3 가지를 해결해야합니다.

HTH