test(ios): add sanity check to diagnose WebView loading issue

Added comprehensive sanity check to diagnose why HTML isn't loading:

Sanity Check:
- Creates simple test.html with red background for visibility
- Checks if bridge and WebView exist
- Attempts to load test.html or index.html directly
- Lists bundle contents to see what files are actually present
- Provides detailed logging of WebView state

Debugging:
- Checks bridge initialization
- Checks WebView existence
- Checks file paths in bundle
- Lists actual bundle contents

Fixes:
- WebView diagnosis: can now see if WebView exists and what URL it has
- File path verification: checks if HTML files are in bundle
- Bundle inspection: lists what files are actually available

Result: Will show exactly why HTML isn't loading - WebView issue, file path issue, or Capacitor config issue
This commit is contained in:
Matthew Raymer
2025-11-11 21:14:37 -08:00
parent abfa1029a4
commit 9582dd8d8c
2 changed files with 87 additions and 0 deletions

View File

@@ -12,6 +12,7 @@
import UIKit
import Capacitor
import WebKit
/**
* Main view controller extending Capacitor's bridge view controller
@@ -22,6 +23,14 @@ class ViewController: CAPBridgeViewController {
override func viewDidLoad() {
super.viewDidLoad()
// SANITY CHECK: Try loading a simple HTML file directly
print("ViewController: Starting sanity check...")
// Wait a moment for Capacitor to initialize, then check
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.performSanityCheck()
}
// Debug: Print bridge configuration
print("ViewController: Bridge initialized")
if let bridge = self.bridge {
@@ -41,6 +50,60 @@ class ViewController: CAPBridgeViewController {
initializePlugin()
}
/**
* Sanity check: Try to load a simple HTML file to verify WebView works
*/
private func performSanityCheck() {
print("ViewController: Performing sanity check...")
// Check if we have a bridge and webView
guard let bridge = self.bridge else {
print("❌ SANITY CHECK FAILED: Bridge is nil!")
return
}
guard let webView = bridge.webView else {
print("❌ SANITY CHECK FAILED: WebView is nil!")
return
}
print("✅ Bridge and WebView exist")
print("WebView URL: \(webView.url?.absoluteString ?? "nil")")
// Try to load test.html directly
if let bundlePath = Bundle.main.resourcePath,
let testHtmlPath = bundlePath.appending("/public/test.html") as String?,
FileManager.default.fileExists(atPath: testHtmlPath) {
let fileURL = URL(fileURLWithPath: testHtmlPath)
print("✅ Found test.html at: \(fileURL.path)")
webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL.deletingLastPathComponent())
} else {
print("❌ test.html not found in bundle")
// Try index.html instead
if let bundlePath = Bundle.main.resourcePath,
let indexHtmlPath = bundlePath.appending("/public/index.html") as String?,
FileManager.default.fileExists(atPath: indexHtmlPath) {
let fileURL = URL(fileURLWithPath: indexHtmlPath)
print("✅ Found index.html at: \(fileURL.path)")
webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL.deletingLastPathComponent())
} else {
print("❌ index.html also not found")
// List what's actually in the bundle
if let bundlePath = Bundle.main.resourcePath {
print("Bundle resource path: \(bundlePath)")
if let contents = try? FileManager.default.contentsOfDirectory(atPath: bundlePath) {
print("Bundle contents: \(contents)")
}
if let publicPath = bundlePath.appending("/public") as String?,
FileManager.default.fileExists(atPath: publicPath),
let publicContents = try? FileManager.default.contentsOfDirectory(atPath: publicPath) {
print("public/ contents: \(publicContents)")
}
}
}
}
}
/**
* Initialize plugin and register native fetcher
* Equivalent to PluginApplication.onCreate() on Android

View File

@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sanity Check</title>
<style>
body {
background: red;
color: white;
font-size: 48px;
text-align: center;
padding: 50px;
margin: 0;
}
</style>
</head>
<body>
<h1>✅ SANITY CHECK PASSED!</h1>
<p>If you see this, the WebView is working!</p>
<p>HTML is loading correctly!</p>
</body>
</html>