我正在尝试遵循this Apple documentation来使用Data Protection。但是,看到它在Swift中,我似乎无法将其转换为C#/ Xamarin。
当然,我不需要整个代码,而只是从哪里开始。特别是这行:
try data.write(to: fileURL, options: .completeFileProtection)
我尝试了几种将其翻译成C#的方法,但是没有用。
有关数据保护的官方Apple文档可用here。
正如Oleg在链接中指出的,如果他们关闭Xamarin论坛,我将在这里进行总结:
public static void SetFileProtection(string filePath)
{
NSMutableDictionary dict = new NSMutableDictionary ();
var protection = new NSString("NSFileProtectionCompleteUnlessOpen");
dict.Add((new NSString("NSFileProtectionKey") as NSObject), (protection as NSObject));
NSError error;
NSFileManager.DefaultManager.SetAttributes(dict, filePath, out error);
if (error != null)
System.Console.WriteLine("SetFileProtection Error: " + error.Description);
}
您可以使用iExplorer测试功能。您将看到,除非该设备受信任,否则将无法访问数据。借助官方文档中的“数据保护类”选择适当的保护级别:
((NSFileProtectionComplete):类密钥受密钥保护从用户密码和设备UID派生。用户之后不久锁定设备(10秒,如果“需要密码”设置为“立即”),解密的类密钥将被丢弃,从而呈现此类中的所有数据在用户再次输入密码或解锁设备之前无法访问使用Touch ID或Face ID。