2016-08-20 3 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

2番目のエラーメッセージは、何が間違っているかを示しています。file:// URLは使用していません。

これは、パス変数からURLを適切に作成することで修正できますが、URLをパススルーし、URL - >パス - > URL変換を行わない方がよいでしょう。パスを使用したすべての操作はURLで直接行うことができます。NSFileManagerNSURLのドキュメントを確認してください。唯一明白でないかもしれないのは、NSFileManagerfileExistsAtPath:ではなくNSURLcheckResourceIsReachableAndReturnError:を使用していますが、慎重にcheckResourceIsReachableAndReturnError:のドキュメントを読んで助言を受けてください。

これらの変更を行うと、報告したエラーの少なくとも3つが解決されます。

HTH

関連する問題