11import SwiftUI
22import UIKit
33import BackgroundTasks
4+ import os. log
45
56class AppDelegate : NSObject , UIApplicationDelegate {
67 var localClient : LocalCredentialClient ! = nil
@@ -14,7 +15,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
1415
1516 // Start the local credential server
1617 LocalCredentialServer . shared. start { success in
17- print ( " AppDelegate: Server start success: \( success) " )
18+ os_log ( . info , log : Logger . server , " Server start success: %{public}@ " , success ? " true " : " false " )
1819 }
1920 return true
2021 }
@@ -23,7 +24,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
2324 // Stop the local credential server
2425 LocalCredentialServer . shared. stop ( )
2526 BackgroundTaskManager . shared. cleanup ( )
26- print ( " AppDelegate: Stopped local credential server" )
27+ os_log ( . info , log : Logger . server , " Stopped local credential server " )
2728 }
2829}
2930
@@ -42,11 +43,11 @@ class BackgroundTaskManager: ObservableObject {
4243 BGTaskScheduler . shared. register ( forTaskWithIdentifier: Self . backgroundTaskIdentifier, using: nil ) { task in
4344 self . handleBackgroundServerMaintenance ( task: task as! BGAppRefreshTask )
4445 }
45- print ( " BackgroundTaskManager: Registered background task: \( Self . backgroundTaskIdentifier) " )
46+ os_log ( . info , log : Logger . app , " Registered background task: %{public}@ " , Self . backgroundTaskIdentifier)
4647 }
4748
4849 func handleAppDidEnterBackground( ) {
49- print ( " BackgroundTaskManager: App entered background, maintaining server" )
50+ os_log ( . info , log : Logger . app , " App entered background, maintaining server " )
5051
5152 // Start background task to keep server running
5253 startBackgroundTask ( )
@@ -56,7 +57,7 @@ class BackgroundTaskManager: ObservableObject {
5657 }
5758
5859 func handleAppWillEnterForeground( ) {
59- print ( " BackgroundTaskManager: App entering foreground" )
60+ os_log ( . info , log : Logger . app , " App entering foreground " )
6061
6162 // End background task if running
6263 endBackgroundTask ( )
@@ -77,27 +78,27 @@ class BackgroundTaskManager: ObservableObject {
7778
7879 do {
7980 try BGTaskScheduler . shared. submit ( request)
80- print ( " BackgroundTaskManager: Scheduled background refresh task" )
81+ os_log ( . info , log : Logger . app , " Scheduled background refresh task " )
8182 } catch {
82- print ( " BackgroundTaskManager: Failed to schedule background refresh: \( error ) " )
83+ os_log ( . error , log : Logger . app , " Failed to schedule background refresh: %{public}@ " , error . localizedDescription )
8384 }
8485 }
8586
8687 private func handleBackgroundServerMaintenance( task: BGAppRefreshTask ) {
87- print ( " BackgroundTaskManager: Handling background server maintenance" )
88+ os_log ( . info , log : Logger . app , " Handling background server maintenance " )
8889
8990 // Schedule next refresh
9091 scheduleBackgroundRefresh ( )
9192
9293 task. expirationHandler = {
93- print ( " BackgroundTaskManager: Background task expired" )
94+ os_log ( . info , log : Logger . app , " Background task expired " )
9495 task. setTaskCompleted ( success: false )
9596 }
9697
9798 // Check and restart server if needed
9899 DispatchQueue . global ( qos: . default) . async {
99100 if !LocalCredentialServer. shared. isRunning {
100- print ( " BackgroundTaskManager: Restarting server during background refresh" )
101+ os_log ( . info , log : Logger . app , " Restarting server during background refresh " )
101102 LocalCredentialServer . shared. start ( )
102103
103104 // Wait a moment for server to start
@@ -113,7 +114,7 @@ class BackgroundTaskManager: ObservableObject {
113114 private func startBackgroundTask( ) {
114115 backgroundTask = UIApplication . shared. beginBackgroundTask ( withName: " LocalServerMaintenance " ) {
115116 [ weak self] in
116- print ( " BackgroundTaskManager: Background task expired, ending task" )
117+ os_log ( . info , log : Logger . app , " Background task expired, ending task " )
117118 self ? . endBackgroundTask ( )
118119 }
119120
@@ -132,21 +133,21 @@ class BackgroundTaskManager: ObservableObject {
132133 }
133134
134135 private func maintainServerInBackground( ) {
135- print ( " BackgroundTaskManager: Maintaining server in background" )
136+ os_log ( . info , log : Logger . app , " Maintaining server in background " )
136137
137138 // Keep the server alive for as long as possible in background
138139 while backgroundTask != . invalid && UIApplication . shared. backgroundTimeRemaining > 5.0 {
139140 // Check server status periodically
140141 if !LocalCredentialServer. shared. isRunning {
141- print ( " BackgroundTaskManager: Restarting server in background" )
142+ os_log ( . info , log : Logger . app , " Restarting server in background " )
142143 LocalCredentialServer . shared. start ( )
143144 }
144145
145146 // Sleep for a short interval
146147 Thread . sleep ( forTimeInterval: 1.0 )
147148 }
148149
149- print ( " BackgroundTaskManager: Background maintenance ending, time remaining: \( UIApplication . shared. backgroundTimeRemaining) " )
150+ os_log ( . info , log : Logger . app , " Background maintenance ending, time remaining: %{public}f " , UIApplication . shared. backgroundTimeRemaining)
150151 }
151152}
152153
@@ -179,19 +180,19 @@ struct Application: App {
179180 private func handleScenePhaseChange( _ phase: ScenePhase ) {
180181 switch phase {
181182 case . active:
182- print ( " Application: Scene became active" )
183+ os_log ( . debug , log : Logger . app , " Scene became active " )
183184 backgroundManager. handleAppWillEnterForeground ( )
184185
185186 case . inactive:
186- print ( " Application: Scene became inactive" )
187+ os_log ( . debug , log : Logger . app , " Scene became inactive " )
187188 // Handle brief inactive state (e.g., incoming call, control center)
188189
189190 case . background:
190- print ( " Application: Scene entered background" )
191+ os_log ( . debug , log : Logger . app , " Scene entered background " )
191192 backgroundManager. handleAppDidEnterBackground ( )
192193
193194 @unknown default :
194- print ( " Application: Unknown scene phase: \( phase) " )
195+ os_log ( . default , log : Logger . app , " Unknown scene phase: %{public}@ " , String ( describing : phase) )
195196 }
196197 }
197198}
0 commit comments