2015-01-01 2 views
2

문서 기반 Mac 응용 프로그램을 샌드 박스 처리했습니다. 내 문서는 사용자 컴퓨터에 이미지를 통합합니다. 내 문서에서 사용 된 이미지에 문서 범위의 책갈피를 저장하여 문서를 닫았다가 다시 열 때 이미지에 액세스 할 수있게하려고합니다.새 문서에 저장하기 위해 문서 유효 범위가 지정된 책갈피는 어떻게 만듭니 까?

//path is path to an image 
//for a new document docUrl is set to the location where we will save our document 
NSURL * pathUrl = [NSURL fileURLWithPath:path]; 
NSError * error; 
NSData * pathBookmarkData = [pathUrl bookmarkDataWithOptions: 
           (NSURLBookmarkCreationWithSecurityScope 
          | NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess) 
           includingResourceValuesForKeys:[NSArray arrayWithObject:NSURLPathKey] 
          relativeToURL:docUrl error:&error]; 

이 다음과 같은 오류가 발생합니다 :

Error Domain=NSCocoaErrorDomain Code=260 "The file “Untitled.mydocext” couldn’t be opened because there is no such file." (Collection URL points to a file that doesn't exist) UserInfo=0x608000070800 {NSURL=file:///Users/myname/Pictures/Untitled.mydocext, NSDebugDescription=Collection URL points to a file that doesn't exist} 

가 어떻게 새 문서에 저장되는 문서의 범위 북마크를 만들려면 어떻게해야합니까 여기

내가 책갈피를 만드는 방법입니까?

NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"png", @"jpg", @"jpeg", @"bmp", @"gif", @"tif", @"tiff", @"PNG", @"JPG", @"JPEG", @"BMP", @"GIF", @"TIF", @"TIFF", nil]; 

NSOpenPanel *panel; 

panel = [NSOpenPanel openPanel]; 
[panel setTitle:@"Select Photos"]; 

[panel setFloatingPanel:YES]; 

[panel setCanChooseDirectories:YES]; 
[panel setCanChooseFiles:YES]; 
[panel setAllowsMultipleSelection:YES]; 
[panel setAllowedFileTypes:fileTypes]; 

[panel beginWithCompletionHandler:^(NSInteger result){ 
    if (result == NSFileHandlingPanelOKButton) { 
     NSMutableArray * pathsArray = [[NSMutableArray alloc] init]; 
     NSArray * urlArray = [panel URLs]; 
     for (NSURL * url in urlArray) { 
      //this is how I get path to image, assume I am not selecting directories 
      NSString * path = [url path]; 
     } 
    } 
}]; 
+0

어떻게 '경로'를 얻었습니까? 선호되는 방법은 NSOpenPanel을 사용하여 보안 유효 URL을 얻거나 사용자가 앱에 이미지를 드롭하는 것입니다. –

+0

사용자가 내 문서를 처음 저장하려고 시도 할 때까지 보안 범위가 지정된 책갈피를 가져 오는 것을 지연합니다. 이 때 문서를 저장할 위치를 얻고 relativeHRL을 bookmarkDataWithOptions : includeResourceValuesForKeys : relativeToURL : error : 함수를 사용하여 문서 범위의 책갈피를 얻습니다. – AmaltasCoder

+0

@AmaltasCoder이 문제를 해결해 봤습니까? 나는 똑같은 일을하려하지만 OSX의 여러 가지 버그로 인해 문서 범위의 책갈피가있는 문서를 올바르게/안정적으로 저장할 수 없습니다. –

답변

0

SSB의 생성을 지연 할 필요가 없습니다 : 여기

내가 경로를 얻는 방법이다. 또한 경로로 전송 된 다음 URL로 전송하면 샌드 박스에 문제가 발생할 수 있습니다. 다음을 따라 무언가를 사용해보십시오 :

// the URLs come back with access to it. they are added to the sandbox by the panel. no need to do anything like start accessing security scoped. 
    NSArray * urlArray = [panel URLs]; 

    for (NSURL *urlToStore in urlArray) { 
     // create a SSB out of the URL 
     NSError *erroer = nil; 
     NSData *bookmarkData = nil; 
     bookmarkData = [urlToStore bookmarkDataWithOptions:(NSURLBookmarkCreationWithSecurityScope| NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess) 
          includingResourceValuesForKeys:nil 
              relativeToURL:docUrl 
                error:&erroer]; 
     if (erroer) { 
      NSLog(@"couldn't create NSURLBookmarkCreationWithSecurityScope with error: %@", [erroer description]); 
     } else if (bookmarkData) { 
      // store the bookmark data either in the document or internally for later persistency 
     } else { 
      NSLog(@"no error and no bookmarkData: %@", [self description]); 
     } 
    }