我的应用已打开数据保护,并且我使用NSFileProtectionComplete
+ (void)createLogFile {
NSString *deviceModel = [Utils getDeviceModel];
NSString *appVersion = [Utils getAppVersion];
NSData *initData = [[NSString stringWithFormat:@"%@-%@\n================================\n\n\n", deviceModel, appVersion] dataUsingEncoding:NSUTF8StringEncoding];
[[NSFileManager defaultManager] createFileAtPath:[self logFilePath]
contents:initData
attributes:@{NSFileProtectionKey: NSFileProtectionComplete}];
}
并且当我锁定设备applicationProtectedDataWillBecomeUnavailable:
时将被调用。
- (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication *)application {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSData *key = [MyKeychain getKey];
NSString *log = [NSString stringWithFormat:@"The key is:\n %@", key];
[MyFileLogger logInfo:log];
});
}
然后我可以在文件中找到结果,这意味着我可以在设备锁定时写入该文件。 设备锁定时,Data Protection不应阻止访问文件吗?怎么了?
-更新-(添加方法logInfo:)
+ (void)logInfo:(NSString *)str {
NSString *info = [self wrapWithTimestamp: str];
NSString *logFilePath = [Utils logFilePath];
if (![[NSFileManager defaultManager] fileExistsAtPath:logFilePath]) {
[Utils createLogFile];
}
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:logFilePath];
[handle truncateFileAtOffset:[handle seekToEndOfFile]];
[handle writeData:[info dataUsingEncoding:NSUTF8StringEncoding]];
[handle closeFile];
}
答案 0 :(得分:1)
根据对this question的回答,在调用applicationProtectedDataWillBecomeUnavailable
方法之后,在激活数据保护之前有10秒的“宽限期”。
如果您将延迟时间从5秒增加到11秒,则应该看到您的数据没有写入日志文件。
我能够通过示例代码和11秒的延迟观察到这一点。