@@ -106,6 +106,10 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
106106 private var waitingForLocation : Bool = false
107107 private var isPresentingPermissionAlert : Bool = false
108108
109+ // Store original webview colors to restore them when stopping
110+ private var originalWebViewBackgroundColor : UIColor ?
111+ private var originalWebViewSubviewColors : [ UIView : UIColor ] = [ : ]
112+
109113 // MARK: - Helper Methods for Aspect Ratio
110114
111115 /// Parses aspect ratio string and returns the appropriate ratio for the current orientation
@@ -141,6 +145,30 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
141145 private func makeWebViewTransparent( ) {
142146 guard let webView = self . webView else { return }
143147
148+ // IMPORTANT: Save colors synchronously FIRST to prevent race condition
149+ // If we don't have saved colors yet, save them now (before going async)
150+ if self . originalWebViewBackgroundColor == nil {
151+ self . originalWebViewBackgroundColor = webView. backgroundColor
152+ self . originalWebViewSubviewColors. removeAll ( )
153+
154+ // Define a recursive function to traverse and save colors
155+ func saveSubviewColors( _ view: UIView ) {
156+ // Save the original background color before changing it
157+ if let bgColor = view. backgroundColor, bgColor != . clear {
158+ self . originalWebViewSubviewColors [ view] = bgColor
159+ }
160+
161+ // Recurse for all subviews
162+ for subview in view. subviews {
163+ saveSubviewColors ( subview)
164+ }
165+ }
166+
167+ // Save all subview colors synchronously
168+ saveSubviewColors ( webView)
169+ }
170+
171+ // Now make the changes asynchronously on main thread
144172 DispatchQueue . main. async {
145173 _ = CFAbsoluteTimeGetCurrent ( )
146174
@@ -170,6 +198,51 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
170198 }
171199 }
172200
201+ private func restoreWebViewBackground( _ webView: UIView ) {
202+ // Restore the saved background colors
203+ func restoreSubviewsBackground( _ view: UIView ) {
204+ // Restore the saved background color for this view
205+ if let savedColor = self . originalWebViewSubviewColors [ view] {
206+ view. backgroundColor = savedColor
207+ } else {
208+ // Fallback: If no saved color, intelligently restore based on view type
209+ let className = String ( describing: type ( of: view) )
210+ if className. contains ( " WKScrollView " ) || className. contains ( " WKContentView " ) {
211+ // Only restore if it's currently clear (meaning we likely made it transparent)
212+ if view. backgroundColor == . clear || view. backgroundColor == nil {
213+ view. backgroundColor = . white
214+ }
215+ }
216+ }
217+
218+ // Recurse for all subviews
219+ for subview in view. subviews {
220+ restoreSubviewsBackground ( subview)
221+ }
222+ }
223+
224+ // Restore the main webview background color
225+ if let originalColor = self . originalWebViewBackgroundColor {
226+ webView. backgroundColor = originalColor
227+ } else {
228+ // Fallback: If no saved color and webview is clear, restore to white
229+ if webView. backgroundColor == . clear || webView. backgroundColor == nil {
230+ webView. backgroundColor = . white
231+ }
232+ }
233+
234+ // Restore all subviews
235+ restoreSubviewsBackground ( webView)
236+
237+ // Clear the saved colors dictionary
238+ self . originalWebViewSubviewColors. removeAll ( )
239+ self . originalWebViewBackgroundColor = nil
240+
241+ // Force a layout pass to apply changes
242+ webView. setNeedsLayout ( )
243+ webView. layoutIfNeeded ( )
244+ }
245+
173246 private func presentCameraPermissionAlert( title: String ,
174247 message: String ,
175248 openSettingsText: String ,
@@ -594,7 +667,13 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
594667 previewView. removeFromSuperview ( )
595668 self . previewView = nil
596669 }
597- self . webView? . isOpaque = true
670+
671+ // Restore webView to opaque state with original background
672+ if let webView = self . webView {
673+ webView. isOpaque = true
674+ // Restore the original background colors that were saved
675+ self . restoreWebViewBackground ( webView)
676+ }
598677
599678 // Force stop the camera controller regardless of state
600679 self . cameraController. stopRequestedAfterCapture = false
@@ -878,7 +957,13 @@ public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelega
878957 self . previewView = nil
879958 }
880959
881- self . webView? . isOpaque = true
960+ // Restore webView to opaque state with original background
961+ if let webView = self . webView {
962+ webView. isOpaque = true
963+ // Restore the original background colors that were saved
964+ self . restoreWebViewBackground ( webView)
965+ }
966+
882967 self . isInitialized = false
883968 self . isInitializing = false
884969
0 commit comments