Files
intaleq/ios/Runner/KeychainHelper.swift
Hamza-Ayed 3742d5b417 25-7-26-1
2025-07-26 10:30:10 +03:00

60 lines
1.7 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// KeychainHelper.swift
// Runner
//
// Created by Hamza Aleghwairyeen on 12/03/2025.
//
import Foundation
import Security
class KeychainHelper {
static let shared = KeychainHelper()
private init() {}
// حفظ البيانات في Keychain
func save(key: String, value: String) {
let data = value.data(using: .utf8)!
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key,
kSecValueData as String: data,
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlockedThisDeviceOnly
]
SecItemDelete(query as CFDictionary) // حذف أي قيمة قديمة بنفس المفتاح
SecItemAdd(query as CFDictionary, nil) // إضافة القيمة الجديدة
}
// استرجاع البيانات من Keychain
func get(key: String) -> String? {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key,
kSecReturnData as String: kCFBooleanTrue!,
kSecMatchLimit as String: kSecMatchLimitOne
]
var dataTypeRef: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &dataTypeRef)
guard status == errSecSuccess, let data = dataTypeRef as? Data else {
return nil
}
return String(data: data, encoding: .utf8)
}
// حذف البيانات من Keychain
func delete(key: String) {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: key
]
SecItemDelete(query as CFDictionary)
}
}