如何在Xamarin.iOS中使用数据保护?

问题描述 投票:1回答:1

我正在尝试遵循this Apple documentation来使用Data Protection。但是,看到它在Swift中,我似乎无法将其转换为C#/ Xamarin。

当然,我不需要整个代码,而只是从哪里开始。特别是这行:

try data.write(to: fileURL, options: .completeFileProtection)

我尝试了几种将其翻译成C#的方法,但是没有用。

c# ios swift xamarin xamarin.ios
1个回答
0
投票

有关数据保护的官方Apple文档可用here

正如Oleg在链接中指出的,如果他们关闭Xamarin论坛,我将在这里进行总结:

  • 首先,您必须为您的App ID启用数据保护并更新您的配置文件。
  • 将您的iOS密码锁定设置为“立即”要求输入密码,然后通过启动您的应用程序然后锁定设备来对其进行测试。
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。

© www.soinside.com 2019 - 2024. All rights reserved.