ios - 如何在 Xamarin.iOS 中保护 cache.db 文件

标签 ios xamarin xamarin.forms xamarin.ios

我想保护 iPhone 上的 cache.db 文件。现在,当我安装我的应用程序并将 iPhone 连接到我的 Mac 时,我能够在应用程序文件结构中看到 cache.db 文件,并且我能够读取基本上包含请求的文件内容和回应。我将 NoCatch 应用于 HTTP 客户端对象。但不是没有区别。

HttpClientObj.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue 
                                                       { 
                                                           NoCache = true 
                                                       };

最佳答案

翻译 the solution here with SWIFT进入 C#:

防止请求和参数写入 Cache.db iOS

如果您使用NSUrlSession:

    var configuration = NSUrlSessionConfiguration.DefaultSessionConfiguration;
    configuration.URLCache = new NSUrlCache(0,0,"");

    NSUrlSession seeion = NSUrlSession.FromConfiguration(configuration);

或者设置在全局NSUrlCache级别:

    NSUrlCache.SharedCache = new NSUrlCache(0,0,"");

更新:

您可以在创建HttpClient时使用 native 处理程序:

    public MainPage()
    {
        InitializeComponent();

        HttpClient httpClient;

        if (Device.RuntimePlatform == Device.iOS)
        {
            var configuration = NSUrlSessionConfiguration.DefaultSessionConfiguration;
            configuration.URLCache = new NSUrlCache(0, 0, "");

            NSUrlSessionHandler handler = new NSUrlSessionHandler(configuration);
            httpClient = new HttpClient(handler);
        }else if (Device.RuntimePlatform == Device.Android)
        {
            AndroidClientHandler handler = new AndroidClientHandler();
            httpClient = new HttpClient(handler);
        }

        //...your request
    }

请记住添加对 Xamarin.iOS.dllMono.Android.dll 的引用。

关于ios - 如何在 Xamarin.iOS 中保护 cache.db 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58870923/

相关文章:

objective-c - 所有属性都应该声明为 "nonatomic"吗?

ios - UIDynamicAnimator,removeAllBehaviors() 第一次不起作用

c# - Xamarin:如何获取光标/触摸坐标(位置 X 和 Y)?

visual-studio - "Class" namespace 中不存在 "http://schemas.microsoft.com/winfx/2009/xaml"属性

ios - 如何正确释放没有句柄的分配对象

ios - 未使用 NSData 形成的 UIImage 对象

ios - 无法摆脱应用程序包中的旧文件夹

c# - 在 Xamarin/Visual Studio 中构建的 native iOS 应用程序打开然后立即关闭

xamarin.forms - 每天在 Xamarin Forms 上运行一次任务

android - Android Q 中的 Landroid/view/LayoutInflater 类中没有字段 mConstructorArgs