Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>

This commit is contained in:
Matt Bruce 2026-01-14 16:17:20 -06:00
parent 980f1c8f15
commit 2a63802b24
11 changed files with 83 additions and 9 deletions

8
.gitignore vendored
View File

@ -2,5 +2,9 @@
.build/
.swiftpm/
DerivedData/
*.xcodeproj/project.xcworkspace/xcuserdata
*.xcodeproj/xcuserdata
# Xcode User Data
xcuserdata/
*.xcodeproj/project.xcworkspace/xcuserdata/
*.xcodeproj/xcuserdata/
*.xcworkspace/xcuserdata/

View File

@ -5,5 +5,6 @@ INFOPLIST_KEY_CFBundleDisplayName = SecureStorage
SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES
INFOPLIST_KEY_BaseBundleID = $(BASE_BUNDLE_ID)
INFOPLIST_KEY_TeamID = $(TEAM_ID)
INFOPLIST_KEY_AppGroupID = $(APP_GROUP_ID)
CODE_SIGN_ENTITLEMENTS = SecureStorageSample/SecureStorageSample.entitlements

View File

@ -8,5 +8,6 @@ WATCHOS_DEPLOYMENT_TARGET = 10.0
SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES
INFOPLIST_KEY_BaseBundleID = $(BASE_BUNDLE_ID)
INFOPLIST_KEY_TeamID = $(TEAM_ID)
INFOPLIST_KEY_AppGroupID = $(APP_GROUP_ID)
CODE_SIGN_ENTITLEMENTS = SecureStorageSample Watch App/SecureStorageSample Watch App.entitlements

View File

@ -12,6 +12,7 @@ import SharedKit
@main
struct SecureStorageSampleApp: App {
init() {
StorageServiceIdentifiers.logConfiguration()
_ = WatchConnectivityService.shared
Task {
// 1. Global Encryption Configuration

View File

@ -8,6 +8,7 @@
import SwiftUI
import LocalData
@MainActor
struct EncryptedStorageDemo: View {
@State private var logEntry = ""
@State private var storedLogs: [String] = []

View File

@ -9,6 +9,7 @@ import SwiftUI
import LocalData
import SharedKit
@MainActor
struct FileSystemDemo: View {
@State private var profileName = ""
@State private var profileEmail = ""
@ -191,7 +192,14 @@ struct FileSystemDemo: View {
Task {
do {
let key = StorageKeys.UserProfileFileKey(directory: selectedDirectory)
storedProfile = try await StorageRouter.shared.get(key)
let profile = try await StorageRouter.shared.get(key)
storedProfile = profile
// Sync to text fields
profileName = profile.name
profileEmail = profile.email
profileAge = profile.age.map(String.init) ?? ""
statusMessage = "✓ Loaded from file system"
} catch StorageError.notFound {
storedProfile = nil
@ -243,7 +251,14 @@ struct FileSystemDemo: View {
Task {
do {
let key = StorageKeys.AppGroupUserProfileKey(directory: selectedDirectory)
appGroupProfile = try await StorageRouter.shared.get(key)
let profile = try await StorageRouter.shared.get(key)
appGroupProfile = profile
// Sync to text fields
profileName = profile.name
profileEmail = profile.email
profileAge = profile.age.map(String.init) ?? ""
appGroupStatusMessage = "✓ App Group loaded from file system"
} catch StorageError.notFound {
appGroupProfile = nil

View File

@ -8,6 +8,7 @@
import SwiftUI
import LocalData
@MainActor
struct KeychainDemo: View {
@State private var username = ""
@State private var password = ""
@ -141,6 +142,11 @@ struct KeychainDemo: View {
)
let credential = try await StorageRouter.shared.get(key)
storedCredential = "Username: \(credential.username)\nPassword: ****"
// Sync to fields
username = credential.username
password = credential.password
statusMessage = "✓ Retrieved from Keychain"
} catch StorageError.notFound {
storedCredential = ""

View File

@ -8,6 +8,7 @@
import SwiftUI
import LocalData
@MainActor
struct PlatformSyncDemo: View {
@State private var settingValue = ""
@State private var storedValue = ""
@ -181,7 +182,9 @@ struct PlatformSyncDemo: View {
availability: selectedPlatform,
syncPolicy: selectedSync
)
storedValue = try await StorageRouter.shared.get(key)
let value = try await StorageRouter.shared.get(key)
storedValue = value
settingValue = value // Sync to field
statusMessage = "✓ Loaded value"
} catch StorageError.notFound {
storedValue = ""

View File

@ -8,6 +8,7 @@
import SwiftUI
import LocalData
@MainActor
struct UserDefaultsDemo: View {
@State private var inputText = ""
@State private var storedValue = ""
@ -155,7 +156,9 @@ struct UserDefaultsDemo: View {
Task {
do {
let key = StorageKeys.AppVersionKey()
storedValue = try await StorageRouter.shared.get(key)
let value = try await StorageRouter.shared.get(key)
storedValue = value
inputText = value // Sync to field
statusMessage = "✓ Loaded successfully"
} catch StorageError.notFound {
storedValue = ""
@ -201,7 +204,9 @@ struct UserDefaultsDemo: View {
Task {
do {
let key = StorageKeys.AppGroupUserDefaultsKey()
appGroupStoredValue = try await StorageRouter.shared.get(key)
let value = try await StorageRouter.shared.get(key)
appGroupStoredValue = value
inputText = value // Sync to field
statusMessage = "✓ App Group loaded successfully"
} catch StorageError.notFound {
appGroupStoredValue = ""

View File

@ -2,9 +2,10 @@ import Foundation
public enum StorageServiceIdentifiers {
public static var bundleIdentifier: String {
Bundle.main.object(forInfoDictionaryKey: "BaseBundleID") as? String ??
let identifier = Bundle.main.object(forInfoDictionaryKey: "BaseBundleID") as? String ??
Bundle.main.bundleIdentifier ??
"com.example.securestorage"
return identifier
}
private static var teamIDPrefix: String {
@ -15,7 +16,17 @@ public enum StorageServiceIdentifiers {
}
public static var appGroupIdentifier: String {
"group.\(bundleIdentifier)"
let identifier = Bundle.main.object(forInfoDictionaryKey: "AppGroupID") as? String ??
"group.\(bundleIdentifier.lowercased())"
return identifier
}
public static func logConfiguration() {
Logger.debug("--- STORAGE CONFIGURATION ---")
Logger.debug("Bundle ID: \(bundleIdentifier)")
Logger.debug("Team ID Prefix: \(teamIDPrefix)")
Logger.debug("App Group ID: \(appGroupIdentifier)")
Logger.debug("---------------------------")
}
public static var keychainCredentials: String {

View File

@ -0,0 +1,26 @@
import Foundation
/// Public logging utility for the SecureStorage workspace.
public enum Logger {
public static var isLoggingEnabled = true
public static func debug(_ message: String) {
#if DEBUG
if isLoggingEnabled {
print(" {SECURE_STORAGE} \(message)")
}
#endif
}
public static func error(_ message: String, error: Error? = nil) {
#if DEBUG
var logMessage = " {SECURE_STORAGE} ❌ \(message)"
if let error = error {
logMessage += " | Error: \(error.localizedDescription)"
}
if isLoggingEnabled {
print(logMessage)
}
#endif
}
}