98b7600797
对标安卓正式版、对接 app-server(8770)的 SwiftUI 客户端。iOS 无障碍受限、无比价核心,做自成一体:美团 CPS 导购 + 福利(签到/任务/金币)+ 查看(比价记录/省钱战绩)。 功能完整 M0-M5:脚手架、短信登录(JWT+Keychain+401 自动刷新)、首页券 feed 导购、比价记录+省钱战绩、福利、我的(资料/反馈/设置/注销)。 单 target、0 三方依赖、XcodeGen 生成工程;Debug/Release 双 plist 分流 ATS。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
54 lines
1.8 KiB
Swift
54 lines
1.8 KiB
Swift
import Foundation
|
|
import Security
|
|
|
|
/// JWT 持久化(对应安卓的 EncryptedSharedPreferences)。Keychain 本身加密 + 系统托管密钥。
|
|
enum KeychainStore {
|
|
enum Key: String {
|
|
case accessToken = "access_token"
|
|
case refreshToken = "refresh_token"
|
|
}
|
|
|
|
private static let service = "com.jishisongfu.shaguabijia.auth"
|
|
|
|
static func get(_ key: Key) -> String? {
|
|
let query: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: service,
|
|
kSecAttrAccount as String: key.rawValue,
|
|
kSecReturnData as String: true,
|
|
kSecMatchLimit as String: kSecMatchLimitOne,
|
|
]
|
|
var item: CFTypeRef?
|
|
guard SecItemCopyMatching(query as CFDictionary, &item) == errSecSuccess,
|
|
let data = item as? Data,
|
|
let str = String(data: data, encoding: .utf8) else {
|
|
return nil
|
|
}
|
|
return str
|
|
}
|
|
|
|
static func set(_ value: String, _ key: Key) {
|
|
let data = Data(value.utf8)
|
|
let base: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: service,
|
|
kSecAttrAccount as String: key.rawValue,
|
|
]
|
|
// upsert:先删再加,避免 duplicate item
|
|
SecItemDelete(base as CFDictionary)
|
|
var attrs = base
|
|
attrs[kSecValueData as String] = data
|
|
attrs[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock
|
|
SecItemAdd(attrs as CFDictionary, nil)
|
|
}
|
|
|
|
static func delete(_ key: Key) {
|
|
let query: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: service,
|
|
kSecAttrAccount as String: key.rawValue,
|
|
]
|
|
SecItemDelete(query as CFDictionary)
|
|
}
|
|
}
|