我如何知道iPhone用户当前是否有密码设置和加密启用? [英] How can I find out if the iPhone user currently has a passcode set and encryption enabled?

查看:160
本文介绍了我如何知道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.

我想弹出一个警告,告诉用户他们必须启用密码并开启数据保护(这需要在pre-4上进行备份和恢复iPhones),然后退出应用程序,如果他们没有启用密码和数据保护。我无法弄清楚反正找到这些设置的状态虽然。如果禁用了数据保护,我发现的所有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天全站免登陆