如何确定 iPhone 用户当前是否设置了密码并启用了加密? [英] How can I find out if the iPhone user currently has a passcode set and encryption enabled?

查看:49
本文介绍了如何确定 iPhone 用户当前是否设置了密码并启用了加密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 iPhone 应用程序,需要对其数据进行加密.我已经学会了如何通过设置 NSFileProtectionComplete 属性来打开文件加密.我还知道如何检查 iPhone 版本以确保它们运行的​​是 iOS 4.0 或更高版本.

I'm writing an iPhone application that requires its data to be encrypted. I've learned how to turn on encryption for files by setting the NSFileProtectionComplete attribute. I also know how to check the iPhone version to make sure they are running iOS 4.0 or better.

不过我已经意识到,如果用户没有选择密码并且没有在设置">常规">密码锁"屏幕上专门启用数据保护,那么数据实际上根本不受保护.

What I've realized though, that if the user has not chosen a passcode and has not specifically enabled data protection on the Settings > General > Passcade Lock screen then the data isn't actually protected at all.

我想弹出一个警告并告诉用户他们必须启用密码并打开数据保护(这需要在 4 之前的 iPhone 上进行备份和恢复),如果他们不这样做,然后退出应用程序启用密码和数据保护.尽管如此,我还是无法弄清楚这些设置的状态.如果禁用数据保护,我发现的所有 API(例如 UIApplication 中的protectedDataAvailable")都会成功通过.

I'd like to pop up a warning and tell the user that they must enable a passcode and turn on data protection (which requires a backup and restore on pre-4 iPhones), and then exit the application if they do not have a passcode and data protection enabled. I can't figure out anyway to find out the state of these settings though. All of the APIs I've found, such as "protectedDataAvailable" in UIApplication all pass with success if data protection is disabled.

推荐答案

免责声明:此答案在 ios 4.3.3 之前有效

如果数据保护开启,新创建的文件默认会有一个nil NSFileProtectionKey.

If data protection is turned on, a newly created file will have a nil NSFileProtectionKey by default.

如果数据保护关闭,新创建的文件默认会有一个 NSFileProtectionNone NSFileProtectionKey.

If data protection is turned off, a newly created file will have a NSFileProtectionNone NSFileProtectionKey by default.

因此,您可以使用以下代码检测文件保护的存在:

Thus, you could detect the presence of file protection with the following code:

NSString *tmpDirectoryPath = 
    [NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
NSString *testFilePath = 
    [tmpDirectoryPath stringByAppendingPathComponent:@"testFile"];
[@"" writeToFile:testFilePath 
      atomically:YES
        encoding:NSUTF8StringEncoding
           error:NULL]; // obviously, do better error handling
NSDictionary *testFileAttributes = 
    [[NSFileManager defaultManager] attributesOfItemAtPath:testFile1Path
                                                     error:NULL];
BOOL fileProtectionEnabled = 
    [NSFileProtectionNone isEqualToString:[testFile1Attributes objectForKey:NSFileProtectionKey]];

这篇关于如何确定 iPhone 用户当前是否设置了密码并启用了加密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆