25-3/18/1

This commit is contained in:
Hamza-Ayed
2025-03-18 14:54:52 +03:00
parent 60548527e1
commit 0126011f0e
154 changed files with 2236 additions and 279 deletions

View File

@@ -0,0 +1,59 @@
//
// 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)
}
}