当前位置: 动力学知识库 > 问答 > 编程问答 >

nsfilemanager - How do I add Data Protection Class Entitlements to my iOS 5 app

问题描述:

I have been all over the web including Apple's dev site but I can't find any instruction on how to set an app's /Documents directory attribute NSFileProtectionKey = NSFileProtectionComplete. I have set the value upon the first time the app runs but is there a way to set it otherwise? Like through some X-Code check box, plist, or other.

Also on a related note: Are any files stored under a directory with the data protection class of NSFileProtectionComplete automatically treated as the same protection class as its directory?

Thanks a bunch,

Fissh

网友答案:

To protect files in your application Documents directory, without any code, you have to add an entitlements.plist to your application.

In your entitlements.plist, add the "DataProtectionClass" key, with the "NSFileProtectionComplete" value. Your files will be automatically protected as long as the device is locked.

Here is an example

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
    <dict>
      <key>DataProtectionClass</key>
      <string>NSFileProtectionComplete</string>
    </dict>
  </plist>
网友答案:

WWDC session 208 breezed over this, I think you create an entitlements file with that key, and you don't need to write code.

I haven't done this yet so I'm not completely sure it works.

网友答案:

This is the hack I came up with until I figure out how to set the /Documents directory protections correctly.

NSError *error;
NSDictionary *attrs = [self attributesOfItemAtPath:DOCUMENTS_FOLDER error:&error];
if(![[attrs objectForKey:NSFileProtectionKey] isEqual:NSFileProtectionComplete])
{
    attrs = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
    BOOL success = [self setAttributes:attrs ofItemAtPath:DOCUMENTS_FOLDER error:&error];
    if (!success) 
        NSLog(@"Set ~/Documents attr NOT successfull");
}

I just call the above code upon loading of the app, to set the documents directory protection if it is not set already.

Fissh

网友答案:

http://devforums.apple.com/message/627887 Developer Forums: Apply DataProtectionClass Entitlement

I found this link, and this Entitlements method currently not working and filed for the bug issue, so we would have to do file by file.

分享给朋友:
您可能感兴趣的文章:
随机阅读: