build: 修复iOS/macOS构建配置,适配macOS Keychain问题
1. 添加Pods依赖配置到Xcode工作区 2. 调整macOS权限配置,临时替换Keychain为shared_preferences 3. 重构secure_storage适配macOS兼容性问题 4. 整理iOS权限配置,移除重复声明 5. 更新插件依赖和Podfile配置
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
/// ============================================================
|
||||
/// 闲言APP — 安全存储
|
||||
/// 创建时间: 2026-04-20
|
||||
/// 更新时间: 2026-04-20
|
||||
/// 更新时间: 2026-05-22
|
||||
/// 作用: flutter_secure_storage 封装,用于敏感数据存储
|
||||
/// 上次更新: 初始创建
|
||||
/// 上次更新: macOS Keychain 兼容性修复,临时使用 shared_preferences 替代
|
||||
/// ============================================================
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// 安全存储键名常量
|
||||
class SecureKeys {
|
||||
@@ -26,6 +28,7 @@ class SecureKeys {
|
||||
///
|
||||
/// 用于存储敏感数据如 Token、密码等,
|
||||
/// 底层使用 Keychain (iOS) / EncryptedSharedPreferences (Android)。
|
||||
/// macOS 临时使用 shared_preferences 避免 Keychain 配置问题
|
||||
class SecureStorage {
|
||||
SecureStorage._();
|
||||
|
||||
@@ -33,29 +36,84 @@ class SecureStorage {
|
||||
accessibility: KeychainAccessibility.first_unlock_this_device,
|
||||
);
|
||||
|
||||
static const _macosOptions = MacOsOptions(
|
||||
usesDataProtectionKeychain: false,
|
||||
accessibility: KeychainAccessibility.first_unlock_this_device,
|
||||
groupId: null,
|
||||
);
|
||||
|
||||
static const FlutterSecureStorage _storage = FlutterSecureStorage(
|
||||
iOptions: _iosOptions,
|
||||
mOptions: _macosOptions,
|
||||
);
|
||||
|
||||
// macOS 临时使用 shared_preferences 替代
|
||||
static SharedPreferences? _prefs;
|
||||
|
||||
static Future<void> _ensurePrefs() async {
|
||||
_prefs ??= await SharedPreferences.getInstance();
|
||||
}
|
||||
|
||||
static bool get _isMacOS => defaultTargetPlatform == TargetPlatform.macOS;
|
||||
|
||||
// ============================================================
|
||||
// 通用读写
|
||||
// ============================================================
|
||||
|
||||
/// 读取
|
||||
static Future<String?> read(String key) => _storage.read(key: key);
|
||||
static Future<String?> read(String key) async {
|
||||
if (_isMacOS) {
|
||||
await _ensurePrefs();
|
||||
return _prefs!.getString(key);
|
||||
} else {
|
||||
return _storage.read(key: key);
|
||||
}
|
||||
}
|
||||
|
||||
/// 写入
|
||||
static Future<void> write(String key, String value) =>
|
||||
_storage.write(key: key, value: value);
|
||||
static Future<void> write(String key, String value) async {
|
||||
if (_isMacOS) {
|
||||
await _ensurePrefs();
|
||||
await _prefs!.setString(key, value);
|
||||
} else {
|
||||
await _storage.write(key: key, value: value);
|
||||
}
|
||||
}
|
||||
|
||||
/// 删除
|
||||
static Future<void> delete(String key) => _storage.delete(key: key);
|
||||
static Future<void> delete(String key) async {
|
||||
if (_isMacOS) {
|
||||
await _ensurePrefs();
|
||||
await _prefs!.remove(key);
|
||||
} else {
|
||||
await _storage.delete(key: key);
|
||||
}
|
||||
}
|
||||
|
||||
/// 是否包含
|
||||
static Future<bool> containsKey(String key) => _storage.containsKey(key: key);
|
||||
static Future<bool> containsKey(String key) async {
|
||||
if (_isMacOS) {
|
||||
await _ensurePrefs();
|
||||
return _prefs!.containsKey(key);
|
||||
} else {
|
||||
return _storage.containsKey(key: key);
|
||||
}
|
||||
}
|
||||
|
||||
/// 清空所有
|
||||
static Future<void> deleteAll() => _storage.deleteAll();
|
||||
static Future<void> deleteAll() async {
|
||||
if (_isMacOS) {
|
||||
await _ensurePrefs();
|
||||
final keys = _prefs!.getKeys();
|
||||
for (final key in keys) {
|
||||
if (key.startsWith('auth_') || key == 'userId') {
|
||||
await _prefs!.remove(key);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
await _storage.deleteAll();
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// 便捷方法
|
||||
|
||||
Reference in New Issue
Block a user