You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS卸载MAUI应用后未清除SecureStorage数据的解决方案求助

解决方案:修改iOS端SecureStorage的Keychain访问属性

MAUI.Essentials的SecureStorage在iOS上默认使用Keychain存储数据,而Keychain的默认配置会导致数据在应用卸载后保留(即使未登录iCloud)。要实现卸载后自动清除数据,需要修改Keychain的访问属性,确保数据仅绑定当前设备且卸载后被删除。

步骤1:自定义iOS端SecureStorage实现

在iOS项目中创建自定义的ISecureStorage实现,指定Keychain的Accessible属性为WhenUnlockedThisDeviceOnly:

using Microsoft.Maui.Storage;
using Security;
using Foundation;
using System.Threading.Tasks;

[assembly: Dependency(typeof(CustomSecureStorage))]
namespace YourAppNamespace.iOS
{
    public class CustomSecureStorage : ISecureStorage
    {
        // 自定义Service ID,确保唯一标识你的应用存储
        private const string ServiceId = "com.yourcompany.yourapp.securestorage";

        public Task<string> GetAsync(string key)
        {
            var query = new SecRecord(SecKind.GenericPassword)
            {
                Service = ServiceId,
                Account = key,
                MatchLimit = SecMatchLimit.One,
                ReturnData = true
            };

            var status = SecKeyChain.QueryAsRecord(query, out var record);
            return Task.FromResult(status == SecStatusCode.Success ? record.Data.ToString() : null);
        }

        public Task RemoveAsync(string key)
        {
            var query = new SecRecord(SecKind.GenericPassword)
            {
                Service = ServiceId,
                Account = key
            };
            SecKeyChain.Remove(query);
            return Task.CompletedTask;
        }

        public Task SetAsync(string key, string value)
        {
            var record = new SecRecord(SecKind.GenericPassword)
            {
                Service = ServiceId,
                Account = key,
                ValueData = NSData.FromString(value),
                // 核心配置:数据仅当前设备可用,卸载后自动删除,不参与iCloud备份
                Accessible = SecAccessible.WhenUnlockedThisDeviceOnly
            };

            var status = SecKeyChain.Add(record);
            // 如果已存在则更新
            if (status == SecStatusCode.DuplicateItem)
            {
                var updateQuery = new SecRecord(SecKind.GenericPassword)
                {
                    Service = ServiceId,
                    Account = key
                };
                SecKeyChain.Update(updateQuery, record);
            }

            return Task.CompletedTask;
        }

        public void RemoveAll()
        {
            var query = new SecRecord(SecKind.GenericPassword)
            {
                Service = ServiceId
            };
            SecKeyChain.Remove(query);
        }
    }
}

步骤2:替换MAUI默认服务

在MauiProgram.cs中注册自定义实现,覆盖默认的ISecureStorage:

using Microsoft.Maui.Storage;
using YourAppNamespace.iOS;

var builder = MauiApp.CreateBuilder();
builder
    .UseMauiApp<App>()
    .ConfigureFonts(fonts =>
    {
        fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    });

// 替换默认的SecureStorage实现
builder.Services.AddSingleton<ISecureStorage, CustomSecureStorage>();

return builder.Build();

关键说明

  • SecAccessible.WhenUnlockedThisDeviceOnly属性确保:
    1. 数据仅在设备解锁时可访问
    2. 数据不会被iCloud备份或同步
    3. 应用卸载后,对应的Keychain数据会被自动清除
  • 测试时需彻底卸载模拟器中的应用(长按图标删除,必要时重置模拟器),避免缓存干扰

内容的提问来源于stack exchange,提问作者Mars

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 05:40:41