为hosts文件创建安全范围的书签

时间:2013-03-17 15:06:36

标签: macos bookmarks entitlements nsopenpanel code-signing-entitlements

我正在尝试为用户的hosts文件创建一个安全范围的书签,以便我的应用程序可以在启动时访问和修改它。我收到以下错误:

  

错误域= NSCocoaErrorDomain代码= 256“文件”hosts“不能   打开。“UserInfo = 0x10011e9f0   {NSURL =文件://本地主机/私有的/ etc /主机}

我的权利文件:

<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>

创建书签的相关代码:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    NSOpenPanel *openPanel = [NSOpenPanel openPanel];
    [openPanel setDirectoryURL:[NSURL fileURLWithPath:@"/private/etc/hosts"]];

    [openPanel beginSheetModalForWindow:[self window]
                      completionHandler:^(NSInteger result){

                          if (result == NSOKButton)
                          {
                              [self saveBookmarkFromURL:[openPanel URL]];
                          }else{
                              printf("YOU MUST SELECT A FILE");
                          }

                      }
     ];
}

- (void)saveBookmarkFromURL:(NSURL *)aURL
{
    NSError *error;
    bookmarkData = [aURL
            bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
            includingResourceValuesForKeys:nil
            relativeToURL:nil
            error:&error];

    if (error) {
        NSLog(@"%@", [error description]);
        printf("\nERROR CREATING BOOKMARK DATA");
    } else {
        [[NSUserDefaults standardUserDefaults] setObject:bookmarkData forKey:@"bookmark"];
        printf("\nBOOKMARK DATA SAVED TO PREF FILE");
    }
}

0 个答案:

没有答案